Skip to content

Conversation

@MadLittleMods
Copy link
Contributor

@MadLittleMods MadLittleMods commented Oct 23, 2025

Be mindful that Synapse can be run alongside other code in the same Python process. We shouldn't clobber other SIGHUP handlers as only one can set at time.

(no clobber)

Background

As part of Element's plan to support a light form of vhosting (virtual host) (multiple instances of Synapse in the same Python process), we're currently diving into the details and implications of running multiple instances of Synapse in the same Python process.

"Per-tenant logging" tracked internally by https://github.com/element-hq/synapse-small-hosts/issues/48

Relevant to logging as we use a SIGHUP to reload log config in Synapse.

Dev notes

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)

@MadLittleMods MadLittleMods changed the title Be mindful of other SIGHUP handlers Be mindful of other SIGHUP handlers in 3rd-party code Oct 23, 2025
Comment on lines -601 to -607
return run_as_background_process( # type: ignore[untracked-background-process]
"sighup",
server_name,
_handle_sighup,
*args,
**kwargs,
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I can tell, we don't need this run_as_background_process(...) layer of indirection.

It was originally introduced as @wrap_as_background_process("sighup") in matrix-org/synapse#8817. The purpose of that PR was to handle SIGHUP on the next reactor tick but we already accomplish that with reactor.callFromThread(...) which was also introduced in that PR.

For reference, @wrap_as_background_process was recently refactored to run_as_background_process in #18670 but it's functionally equivalent to other PR it was introduced in.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess this added a logcontext for the sighup callbacks to run in.

I've updated to add a logcontext ✅

Comment on lines +590 to +594
# Register for the SIGHUP signal, chaining any existing handler as there can
# only be one handler per signal and we don't want to clobber any existing
# handlers (like the `multi_synapse` shard process in the context of Synapse Pro
# for small hosts)
previous_sighup_handler = signal.signal(signal.SIGHUP, run_sighup)
Copy link
Contributor Author

@MadLittleMods MadLittleMods Oct 24, 2025

Choose a reason for hiding this comment

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

This is part of the main fix of this PR. Chain any existing SIGHUP handlers like the one we'll see from running in the context of the multi_synapse shard with Synapse Pro for small hosts.

Comment on lines +556 to +559
global _already_setup_sighup_handling
# We only need to set things up once per process.
if _already_setup_sighup_handling:
return
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Other part of the main fix is only setting up the SIGHUP handling once

@MadLittleMods MadLittleMods force-pushed the madlittlemods/no-clobber-sighup-handler branch from 680594e to 63b3d3f Compare October 24, 2025 01:12
@MadLittleMods MadLittleMods marked this pull request as ready for review October 24, 2025 02:54
@MadLittleMods MadLittleMods requested a review from a team as a code owner October 24, 2025 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants