-
Notifications
You must be signed in to change notification settings - Fork 395
Introduce Clock.call_when_running(...)
to include logcontext by default
#18944
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
Merged
MadLittleMods
merged 12 commits into
develop
from
madlittlemods/hs-clock-utils-logcontext
Sep 22, 2025
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
e45dd4f
Split out JSON and `Clock` utilities to avoid circular imports
MadLittleMods bccd224
Fix imports
MadLittleMods 2d1331f
Add `Clock.call_when_running(...)`
MadLittleMods f1e6887
Add lint to prefer `Clock.call_when_running`
MadLittleMods 12e7cf4
Fix `reactor.callWhenRunning` lints
MadLittleMods 731f36e
Remove manual `LoggingContext`
MadLittleMods 0f8076a
Fix copy-paste typos
MadLittleMods 6938134
We can't assume sentinel context when callback is called
MadLittleMods 7b83c9f
Add changelog
MadLittleMods 62f93de
Better changelog
MadLittleMods 19a4f8c
Fix lints
MadLittleMods 4a9730e
Fix wrong config typo
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Introduce `Clock.call_when_running(...)` to wrap startup code in a logcontext, ensuring we can identify which server generated the logs. | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,6 +68,12 @@ | |
category="per-homeserver-tenant-metrics", | ||
) | ||
|
||
PREFER_SYNAPSE_CLOCK_CALL_WHEN_RUNNING = ErrorCode( | ||
"prefer-synapse-clock-call-when-running", | ||
"`synapse.util.Clock.call_when_running` should be used instead of `reactor.callWhenRunning`", | ||
category="synapse-reactor-clock", | ||
) | ||
|
||
|
||
class Sentinel(enum.Enum): | ||
# defining a sentinel in this way allows mypy to correctly handle the | ||
|
@@ -229,9 +235,43 @@ def get_method_signature_hook( | |
): | ||
return check_is_cacheable_wrapper | ||
|
||
if fullname in ( | ||
"twisted.internet.interfaces.IReactorCore.callWhenRunning", | ||
"synapse.types.ISynapseThreadlessReactor.callWhenRunning", | ||
"synapse.types.ISynapseReactor.callWhenRunning", | ||
): | ||
return check_call_when_running | ||
|
||
return None | ||
|
||
|
||
def check_call_when_running(ctx: MethodSigContext) -> CallableType: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This lint pattern is coming from #18828 where we're also going to lint |
||
""" | ||
Ensure that the `reactor.callWhenRunning` callsites aren't used. | ||
|
||
`synapse.util.Clock.call_when_running` should always be used instead of | ||
`reactor.callWhenRunning`. | ||
|
||
Since `reactor.callWhenRunning` is a reactor callback, the callback will start out | ||
with the sentinel logcontext. `synapse.util.Clock` starts a default logcontext as we | ||
want to know which server the logs came from. | ||
|
||
Args: | ||
ctx: The `FunctionSigContext` from mypy. | ||
""" | ||
signature: CallableType = ctx.default_signature | ||
ctx.api.fail( | ||
( | ||
"Expected all `reactor.callWhenRunning` calls to use `synapse.util.Clock.call_when_running` instead. " | ||
"This is so all Synapse code runs with a logcontext as we want to know which server the logs came from." | ||
), | ||
ctx.context, | ||
code=PREFER_SYNAPSE_CLOCK_CALL_WHEN_RUNNING, | ||
) | ||
|
||
return signature | ||
|
||
|
||
def analyze_prometheus_metric_classes(ctx: ClassDefContext) -> None: | ||
""" | ||
Cross-check the list of Prometheus metric classes against the | ||
|
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
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.
This PR can be reviewed commit-by-commit. There are lots of changes because I split out JSON and Clock utilities to avoid circular imports.