fix(mp_logger): deterministic logger socket drain on shutdown (Phase-3 R5)#1199
Draft
vringar wants to merge 1 commit into
Draft
fix(mp_logger): deterministic logger socket drain on shutdown (Phase-3 R5)#1199vringar wants to merge 1 commit into
vringar wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1199 +/- ##
==========================================
+ Coverage 62.14% 62.33% +0.18%
==========================================
Files 40 40
Lines 3918 3961 +43
==========================================
+ Hits 2435 2469 +34
- Misses 1483 1492 +9 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
What
Replaces the
time.sleep(3)wait-and-hope inMPLogger._start_listenerwith a deterministic close-and-await-drain of the loggingServerSocket, so no log records are lost on shutdown and there is no fixed-duration stall.This is Phase-3 R5 of the socket-reliability thread (the timeout audit's one clean, design-fork-free REPLACE item). It is self-contained and independent of the bidi extension back-channel.
The problem
On shutdown,
MPLoggerclosed the listening socket and then slept 3 seconds, hoping any in-flight client messages had landed insocket.queuebefore draining it:socket.close()only closes the listening socket; the per-connection handler threads stay blocked inrecv()on their own client sockets. Too short ⇒ tail log records dropped; too long ⇒ shutdown stalls.The drain handshake
Added
ServerSocket.shutdown(timeout=None)tosocket_interface.py, which makes the drain deterministic:_shutting_downflag and make a throwaway self-connection to wake theaccept()loop (closing the listening socket alone does not reliably unblock a thread parked inaccept()), thenjointhe accept thread. No new connections are accepted past this point.shutdown(SHUT_RDWR)every live client socket, forcing each handler thread blocked inrecv()to return and exit.joinall handler threads.ServerSocketnow tracks its accept thread, its per-connection handler threads, and the live client sockets to make this possible. Each handler puts every fully-received message ontoself.queuebefore exiting, so onceshutdown()returns the queue holds every such message — the caller drains it with no risk of losing a tail record and no fixed wait.MPLogger._start_listenernow callssocket.shutdown()then drains; thesleep(3)and the now-unusedtimeimport are gone.Tests
test/test_socket_interface.py:test_shutdown_drains_all_sent_records— 500 messages sent then client closed; all 500 present aftershutdown().test_shutdown_with_open_client_connection— client left open;shutdown()still drains and all threads exit (this caught a real hang where the accept thread did not wake on a bareclose()).test_shutdown_no_clients— drain is safe when nothing connected.test/test_mp_logger.py(4 tests) still pass; shutdown completes promptly.pytest test/test_socket_interface.py test/test_mp_logger.py→ 7 passed.pre-commit(isort/black/mypy) green.Browser-dependent tests are env-blocked locally (FF150 vs 152) and left to CI.
Scope
Does not touch R1–R4 from the timeout audit (those are needs-human / depend on the bidi back-channel).