fix: escape undecodable subprocess output bytes and deflake graceful shutdown test#277
Merged
epmog merged 1 commit intoJul 21, 2026
Conversation
…shutdown test Follow-up to OpenJobDescription#275, which decoded subprocess stdout/stderr with errors="replace" to keep the stream-reader thread alive when a child process emits non-UTF-8 (locale-encoded) bytes. Switch the decode error handler from "replace" to "backslashreplace" so undecodable bytes are escaped (e.g. b"\xc7" -> "\xc7") instead of collapsed to U+FFFD. This keeps the reader alive exactly the same way while preserving the original byte values in the logs, which helps identify the codepage a DCC batch renderer is emitting. Add integration tests covering single invalid bytes, consecutive invalid bytes, cp1252 text runs, truncated UTF-8 sequences, and that valid multi-byte UTF-8 passes through unescaped. Also fix the flaky test_graceful_shutdown integration test that failed on the Python 3.12 macOS CI job for OpenJobDescription#275. The test slept a fixed 0.5s after spawning fake_client.py before sending SIGTERM; on a slow CI host the signal could arrive before the client registered its handler, killing the process with empty output. The client now prints a ready line after handler registration and the test blocks on it before signaling. Signed-off-by: David Leong <116610336+leongdl@users.noreply.github.com>
leongdl
commented
Jul 21, 2026
| test_client = FakeClient("1234") | ||
| # Signal handler registration (in the main thread) happens in the constructor above. | ||
| # Tests wait for this line before sending signals to avoid a startup race. | ||
| print("client ready", flush=True) |
Contributor
Author
There was a problem hiding this comment.
100 out of 100 runs passed, zero failures.
Each iteration was a fresh pytest invocation of test_graceful_shutdown on Python 3.12, so every run spawned a new fake_client.py subprocess and exercised the full spawn → handshake → SIGTERM → assert cycle independently. Under the old fixed-sleep approach this is exactly the loop that would eventually hit the race; with the client ready handshake the signal can never arrive before the handler is registered, so the fix is deterministic rather than just statistically better.
epmog
approved these changes
Jul 21, 2026
epmog
enabled auto-merge (squash)
July 21, 2026 17:10
jericht
approved these changes
Jul 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #275.
What was the problem/requirement? (What/Why)
Two things left over from #275:
errors="replace"discards the original byte value. The review on fix: decode subprocess output with errors="replace" to keep the reader thread alive #275 recommendedbackslashreplaceinstead: it keeps the stream-reader thread alive exactly the same way, but renders an undecodable byte as\xc7instead of�, preserving the byte value in the logs. That is genuinely useful when debugging which codepage a DCC batch renderer is emitting — the very scenario that motivated the original fix — and it is what CPython itself uses for its own stderr.test_graceful_shutdownis flaky. The Python 3.12 macOS CI job on fix: decode subprocess output with errors="replace" to keep the reader thread alive #275 failed (attempt 2) withAssertionError: assert 'Received SIGTERM signal.' in ''and only passed on re-run. The test spawnedfake_client.py, slept a fixed 0.5 s, then sent SIGTERM. On a slow CI host the signal can arrive before the client registers its signal handler inHTTPClientInterface.__init__, so the default handler kills the process and stdout is empty. The failure is timing-dependent, not 3.12-specific.What was the solution? (How)
Popendecode error handler in_logging_subprocess.pyfromreplacetobackslashreplace, expanded the call-site comment (including a note thatencoding/errorsalso apply to stdin), and updated the threePopenargument assertions in the unit tests.fake_client.pynow prints a flushedclient readyline after its constructor (which registers the signal handler) returns, and the test blocks onstdout.readline()for that line before sending the signal.Also removed the redundant
p._cleanup_io_threads()call in the regression test from #275 —p.wait()already performs the cleanup on every path.What is the impact of this change?
Undecodable bytes in subprocess output now appear in logs as their escaped byte value (e.g.
\xc7) instead of the replacement character, so no forensic information is lost. The graceful-shutdown integration test no longer depends on a fixed startup sleep.How was this change tested?
New parametrized integration test
test_non_utf8_output_is_escapedcovering: a single cp1252 byte (0xc7, observed from3dsmaxbatch.exe),0xff(never valid in UTF-8), consecutive invalid bytes, a cp1252 two-byte run, and a truncated UTF-8 multi-byte sequence. Each case asserts the exact escaped form is logged, the full line is preserved, and noU+FFFDappears.New integration test
test_valid_utf8_is_not_escapedasserting valid multi-byte UTF-8 (héllo wörld Ç 星期五) passes through unmodified.Updated the fix: decode subprocess output with errors="replace" to keep the reader thread alive #275 regression test to assert the escaped form.
Ran the previously flaky
test_graceful_shutdown10× consecutively on Python 3.12 — stable.Full suite on Python 3.12: 482 passed, 26 skipped.
ruff,black --check, andmypyall clean.Have you run the unit tests? Yes.
Was this change documented?
The comment at the
Popencall site was updated to explain the escaping behavior and the stdin caveat. No public docstrings needed changes.Is this a breaking change?
No. The only behavioral change is that previously-replaced undecodable bytes are now rendered as escaped byte values in logs.
Does this change impact security?
No.
backslashreplaceonly affects invalid byte sequences; it cannot synthesize newlines or control characters, and it does not change any trust boundary.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.