Skip to content

tests: server logging via tempfile may have threading issues #1196

Description

@jku

Filing this after debugging rare errors in #1192

The tempfile approach to capturing server logging works fine in current develop branch but it triggers issues in PR 1192 where we start reading from the file at a rapid pace -- based on the results this seems to be a threading issue in file object as the log contents become corrupted (rarely but still reproducibly)

I just tried another approach (worker thread + Queue instead of a TemporaryFile) and it seems to work (this is completely unintegrated into the tests, just an example):

try:
importqueueexceptImportError:
importQueueasqueue# python2importsubprocessimportsysimportthreadingimporttime# Worker function to run in separate thread# Reads from 'stream' (stdout), puts lines in Queue 'line_queue' (Queue is thread-safe)deflog_queue_worker(stream, line_queue):
whileTrue:
log_line=stream.readline()
line_queue.put(log_line)
iflen(log_line) ==0:
# EOSbreak# Start child processproc=subprocess.Popen([sys.executable, "-u", "proxy_server.py"],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
# Run log_queue_worker() in a thread# The thread will exit when child process dieslog_queue=queue.Queue()
log_thread=threading.Thread(target=log_queue_worker, args=(proc.stdout, log_queue))
log_thread.daemon=Truelog_thread.start()
# setup timeout variablesstart=time.time()
elapsed=0timeout=10# Get lines from log_queue until port is found, child process exits, or we timeoutwhileTrue:
try:
line=log_queue.get(timeout=timeout-elapsed)
iflen(line) ==0:
print ("process exit")
breakelifline.startswith(b"bind succeeded, server port is: "):
print (line.rstrip('\n'))
breakexceptqueue.Empty:
print("timeout")
breakfinally:
elapsed=time.time() -start

This has the added benefit of not needing a single sleep (because the worker thread can block and main thread uses Queue.get() timeout)...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions