-
Notifications
You must be signed in to change notification settings - Fork 404
Fix run_as_background_process messing with the caller logging context
#18887
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix run_as_background_process messing with the caller logging context
#18887
Conversation
run_as_background_process messing with the caller contextrun_as_background_process messing with the caller logging context
| @@ -0,0 +1 @@ | |||
| Fix `run_as_background_process` messing with the caller logging context. | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No fix has been added yet. Only tests to reproduce the problem so far
| async def test_run_as_background_process(self) -> None: | ||
| """ | ||
| Test that using `run_as_background_process` doesn't mess with the logging | ||
| context of the caller. | ||
| """ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To sanity check myself, would you also expect these tests to pass in an ideal world?
We currently fallback to the sentinel context at the end within the main block.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd expect the logcontexts to be messed up by await bg_process_d.
run_as_background_process takes an awaitable that follows the logcontext rules and makes it so that you can safely not await on them. This means that the returned deferred doesn't actually follow the logcontext rules itself, and so if you then want to await on the deferred again you must use make_deferred_awaitable (or similar) to make it follow the rules again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, totally makes sense why it's broken in this test case! I jumped the gun on thinking I reproduced the problem here.
In terms of where this PR is spawning from with the problems in #18870; from a pairing session with @erikjohnston, the main thing I was banging my head against was that I removed PreserveLoggingContext at the following spot in order to maintain the main logging context further into Synapse starting up and running. But this also meant leaking the main logging context into the Twisted reactor event loop and apply to the next task which appeared like the desired result because the next task was start but also meant it got messed up later down the line.
Lines 196 to 204 in 4b43e6f
| with PreserveLoggingContext(): | |
| if daemonize: | |
| assert pid_file is not None | |
| if print_pidfile: | |
| print(pid_file) | |
| daemonize_process(pid_file, logger) | |
| run() |
While I haven't explored this thread to its' end, I think this is the key insight into why I was chasing ghosts.
See #18870 (comment) for the other discussion around this.
Thank you @erikjohnston for hopping on a call 🙇
Fix
run_as_background_processmessing with the caller logging contextSpawning from #18870
Testing strategy
There seems to be some test pollution with logging context as running all
tests.util.test_logcontext.LoggingContextTestCasetests will only result in one of these failing. But if you run them individually, they both fail (without a fix):SYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.util.test_logcontext.LoggingContextTestCase.test_run_as_background_processSYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.util.test_logcontext.LoggingContextTestCase.test_run_as_background_process_make_deferred_yieldableTodo
Dev notes
Synapse log context docs:
docs/log_contexts.mdPull Request Checklist
EventStoretoEventWorkerStore.".code blocks.