Skip to content

Conversation

devonh
Copy link
Member

@devonh devonh commented Aug 15, 2025

This PR aims to allow for a clean shutdown of the SynapseHomeServer object so that it can be fully deleted and cleaned up by garbage collection without shutting down the entire python process.

Fix https://github.com/element-hq/synapse-small-hosts/issues/50

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)

Args:
deferred: The Deferred to potentially timeout.
timeout: Timeout in seconds
cancel_on_shutdown: Whether this call should be tracked for cleanup during
Copy link
Contributor

@MadLittleMods MadLittleMods Sep 29, 2025

Choose a reason for hiding this comment

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

Do we still use this anywhere? Why?

Feels like it should be removed


Previous conversation: #18828 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. There are two instances of:

# We don't track these calls since they are constantly being
# overridden by new calls to /sync and they don't hold the
# `HomeServer` in memory on shutdown. It is safe to let them
# timeout of their own accord after shutting down since it
# won't delay shutdown and there won't be any adverse
# behvaviour.

One for clock.call_later and one for timeout_deferred that I have found during local testing to not be worth tracking for the reasons mentioned in the comment.
They fire off every time a /sync response finishes (with new data or timeout), which is quite frequently and will scale to massive number of call_later/cancel flip flops on large homeservers such as matrix.org. There is no need to track them since they don't hold a hs ref. I think the tradeoff here of not tracking them is the right call here based on what I have seen while testing.

@devonh devonh merged commit 396de65 into develop Oct 1, 2025
76 of 78 checks passed
@devonh devonh deleted the devon/clean-shutdown branch October 1, 2025 02:42
MadLittleMods added a commit that referenced this pull request Oct 3, 2025
This was originally removed in #18886
but it looks like it snuck back in #18828
during a bad merge
Comment on lines +616 to +620
# Register background tasks required by this server. This must be done
# somewhat manually due to the background tasks not being registered
# unless handlers are instantiated.
if self.config.worker.run_background_tasks:
self.start_background_tasks()
Copy link
Contributor

@MadLittleMods MadLittleMods Oct 3, 2025

Choose a reason for hiding this comment

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

Looks like this was added as a result of a bad merge conflict. It was removed in #18886 and should still be removed.

Resolving this in #19013

No worries as I know a lot of changes rolled through while you worked on this PR ⏩

MadLittleMods added a commit that referenced this pull request Oct 7, 2025
This was originally removed in
#18886 but it looks like it
snuck back in #18828 during a
[bad
merge](4cd3d91).

Noticed while looking at Synapse setup and startup (just by happen
stance).

I don't think this has adverse effects on Synapse actually working and
`start_background_tasks()` can be called multiple times.


### Is there a good way to audit all of these merges?

As I would like to see the conflicts for each merge.

This works but it's still hard to notice anything is wrong:

```
git log --remerge-diff <commit-sha>
```

> shows the difference from mechanical merge result and the result that
is actually recorded in a merge commit

via
https://stackoverflow.com/questions/15277708/how-do-you-see-show-a-git-merge-conflict-resolution-that-was-done-given-a-mer/71181334#71181334

The following better. Specify the version range to the commit right
before the merge to the merge. And can even specify which file to look
at to make it more obvious with the hindsight we have now.

```
git log --remerge-diff <merge-commit-sha>~1..<merge-commit-sha> -- synapse/server.py
```

Example:
```
git log --remerge-diff 4cd3d91~1..4cd3d91 -- synapse/server.py
```
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.

2 participants