Skip to content

Keep FileWriter lines intact when several threads write - #102

Merged
ecbadeaux merged 1 commit into
Netflix:mainfrom
kwy404:write-each-line-in-one-call
Nov 2, 2026
Merged

ecbadeaux merged 1 commit into
Netflix:mainfrom
kwy404:write-each-line-in-one-call

Conversation

@kwy404

@kwy404 kwy404 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Services that record metrics from several threads while spectator-py writes to stdout, stderr or a file:// location get corrupted protocol lines whenever two threads write at the same moment, so those updates are lost or misread.

Root cause

FileWriter.write calls print(line, file=self._file). print() writes the text and the end newline as two separate write() calls, so another thread can write in between. The output then contains glued lines followed by an empty line:

c:t0,x=y:1c:t1,x=y:1

With 4 threads each incrementing their own counter 50,000 times on a file:// registry, 6 of the 200,000 lines came out malformed on Python 3.12. With this change all 200,000 lines are well formed.

Fix

Write the line and its newline in a single write() call.

Test

test_file_writer_writes_each_line_in_one_call wraps write on the redirected stdout and checks that FileWriter.write("foo") makes exactly one write("foo\n") call. Before the change it records [call('foo'), call('\n')] and fails; after the change it passes. pylint is clean on the changed files.

FileWriter.write used print(), which writes the line and the trailing
newline in two separate calls. Another thread can write between them,
so concurrent metric updates sent to stdout, stderr or a file produced
glued lines such as "c:a:1c:b:1" followed by an empty line. Writing the
line and its newline in one call keeps each protocol line whole.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants