Skip to content

Conversation

MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Oct 1, 2025

Revert #18849

Go back to our custom LogContextScopeManager after trying OpenTracing's ContextVarsScopeManager.

Fix #19004

Why revert?

For reference, with the normal reactor, ContextVarsScopeManager worked just as good as our custom LogContextScopeManager as far as I can tell (and even better in some cases). But since Twisted appears to not fully support ContextVar's, it doesn't work as expected in all cases. Compounding things, ContextVarsScopeManager was causing errors with the experimental SYNAPSE_ASYNC_IO_REACTOR option.

Since we're not getting the full benefit that we originally desired, we might as well revert and figure out alternatives for extending the logcontext lifetimes to support the use case we were trying to unlock (c.f. #18804).

See #19004 (comment) for more info.

Does this require backporting and patch releases?

No. Since ContextVarsScopeManager operates just as good with the normal reactor and was only causing actual errors with the experimental SYNAPSE_ASYNC_IO_REACTOR option, I don't think this requires us to backport and make patch releases at all.

Pull Request Checklist

  • Pull request is based on the develop branch
  • Pull request includes a changelog file. The entry should:
    • Be a short description of your change which makes sense to users. "Fixed a bug that prevented receiving messages from other servers." instead of "Moved X method from EventStore to EventWorkerStore.".
    • Use markdown where necessary, mostly for code blocks.
    • End with either a period (.) or an exclamation mark (!).
    • Start with a capital letter.
    • Feel free to credit yourself, by adding a sentence "Contributed by @github_username." or "Contributed by [Your Name]." to the end of the entry.
  • Code style is correct (run the linters)

This reverts commit 27fc338.

Conflicts:
	synapse/logging/context.py
	tests/logging/test_opentracing.py
…_available`

This test is still expected to fail with our current `LogContextScopeManager`
implementation but there were some flaws in how we were handling
the logcontext before (like pumping the reactor in a non-sentinel logcontext)
own previous custom `_LogContextScope.close(...)` would clear
`LoggingContext.scope` preventing further tracing spans from having the correct
parent.
"""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think ideally this test should work and it would be nice to keep.

It's expected to fail with our current implementation of LogContextScopeManager so it makes sense to remove in the revert.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exploring if we can keep this test and get this functionality with LogContextScopeManager in #19008

This functionality was originally introduced in
#18932
but previously worked fine because we were using
the `ContextVarsScopeManager` which had no problem
tracking the tracing scope across logcontext boundaries.
Now that we've switched back to our own custom
`LogContextScopeManager` which tracks the tracing scope
in the logcontext itself, we need to get the active
tracing span before we switch logcontext. (we switch
to the sentinel logcontext because of the `PreserveLoggingContext()`)
Comment on lines +270 to +274
# Since we track the tracing scope in the `LoggingContext`, before we move to the
# sentinel logcontext (or a new new `LoggingContext`), grab the currently active
# tracing span (if any) so that we can create a cross-link to the background process
# trace.
original_active_tracing_span = active_span(tracer=test_only_tracer)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to maintain the functionality introduced in #18932 (cross-links between the background process trace and currently active trace), we need this change.

Previously, when we were using ContextVarsScopeManager, it tracked the tracing scope across the logcontext changes without issue. Now that we're using our own custom LogContextScopeManager again, we need to capture the active span from the logcontext before we reset to the sentinel context because of the PreserveLoggingContext() below

["fixture_awaitable_return_func"],
)

async def test_run_as_background_process_standalone(self) -> None:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some tests to ensure we maintain the run_as_background tracing behavior regardless of the tracing scope manager we use.

@MadLittleMods MadLittleMods marked this pull request as ready for review October 2, 2025 05:30
@MadLittleMods MadLittleMods requested a review from a team as a code owner October 2, 2025 05:30
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.

ContextVar with tracing breaks responses
1 participant