Skip to content

#12514 Fix MemoryReactor.callWhenRunning not invoking callbacks if already started - #12514

Merged
adiroiban merged 4 commits into
twisted:trunkfrom
MadLittleMods:madlittlemods/fix-memory-reactor-call-when-running
Oct 1, 2025
Merged

adiroiban merged 4 commits into
twisted:trunkfrom
MadLittleMods:madlittlemods/fix-memory-reactor-call-when-running

Conversation

@MadLittleMods

@MadLittleMods MadLittleMods commented Sep 30, 2025 •

Copy link
Copy Markdown
Contributor

Fix MemoryReactor.callWhenRunning not invoking callbacks if already started

(noticed while working on element-hq/synapse#18903)

Scope and purpose

Previously, MemoryReactor.callWhenRunning wouldn't invoke any of the callbacks if the reactor had already started. It did invoke callbacks collected before the reactor started running.

The interface, IReactorCore.callWhenRunning, describes how it should be behaving:

def callWhenRunning(
callable: Callable[..., Any], *args: object, **kwargs: object
) -> Optional[Any]:
"""
Call a function when the reactor is running.
If the reactor has not started, the callable will be scheduled
to run when it does start. Otherwise, the callable will be invoked
immediately.
@param callable: the callable object to call later.
@param args: the arguments to call it with.
@param kwargs: the keyword arguments to call it with.
@return: None if the callable was invoked, otherwise a system
event id for the scheduled call.
"""

This also matches how ReactorBase implements things:

def callWhenRunning(
self, callable: Callable[..., Any], *args: object, **kwargs: object
) -> Optional[_SystemEventID]:
"""
See twisted.internet.interfaces.IReactorCore.callWhenRunning.
"""
if self.running:
callable(*args, **kwargs)
return None
else:
return self.addSystemEventTrigger(
"after", "startup", callable, *args, **kwargs
)


Related to #3025

Dev notes

https://docs.twisted.org/en/latest/development/dev-process.html

$ git clone https://github.com/twisted/twisted twisted
$ python3 -m venv ./venv
$ . venv/bin/activate
$ pip install -e '.[dev]'
$ trial twisted.internet.test.test_testing.ReactorTests

Contributor Checklist:

This process applies to all pull requests - no matter how small.
Have a look at our developer documentation before submitting your Pull Request.

Below is a non-exhaustive list (as a reminder):

  • The title of the PR should describe the changes and starts with the associated issue number, like “#9782 Remove twisted.news. #1234 Brief description”.
  • A release notes news fragment file was create in src/twisted/newsfragments/ (see: Release notes fragments docs.)
  • The automated tests were updated.
  • Once all checks are green, request a review by leaving a comment that contains exactly the string please review.
    Our bot will trigger the review process, by applying the pending review label
    and requesting a review from the Twisted dev team.

@codspeed

codspeed Bot commented Sep 30, 2025 •

Copy link
Copy Markdown

CodSpeed Performance Report

Merging #12514 will not alter performance

Comparing MadLittleMods:madlittlemods/fix-memory-reactor-call-when-running (e795aa0) with trunk (ef6160a)

Summary

✅ 36 untouched

Comment thread src/twisted/internet/testing.py Outdated
Comment on lines +677 to +679
if self.running:
callable(*args, **kw)
return None

@MadLittleMods MadLittleMods Sep 30, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should we additionally append to self.whenRunningHooks in this case?

I'm guessing we may want to always append to self.whenRunningHooks to avoid breaking people's tests that rely on this behavior 🤷

As an aside, it's weird that we will invoke the hooks multiple times whenever reactor.run() is called because we never clear out self.whenRunningHooks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that whenRunningHooks should have been private and people should not depend on it.

Also... when reactor.run() is called, I think that whenRunningHooks should be cleared...

I don't know what to say.

My call is to go with this implementation...and we can see if people will complain

Comment thread src/twisted/internet/testing.py Outdated
Comment on lines +677 to +681
if self.running:
callable(*args, **kw)
return None
else:
self.whenRunningHooks.append((callable, args, kw))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

For reference, this matches the ReactorBase.callWhenRunning implementation:

def callWhenRunning(
self, callable: Callable[..., Any], *args: object, **kwargs: object
) -> Optional[_SystemEventID]:
"""
See twisted.internet.interfaces.IReactorCore.callWhenRunning.
"""
if self.running:
callable(*args, **kwargs)
return None
else:
return self.addSystemEventTrigger(
"after", "startup", callable, *args, **kwargs
)

Comment thread src/twisted/internet/testing.py Resolved
@MadLittleMods
MadLittleMods marked this pull request as ready for review September 30, 2025 20:39
@chevah-robot
chevah-robot requested a review from a team September 30, 2025 20:39
@MadLittleMods

MadLittleMods commented Sep 30, 2025 •

Copy link
Copy Markdown
Contributor Author

please review

Once all checks are green, request a review by leaving a comment that contains exactly the string please review.
Our bot will trigger the review process, by applying the pending review label
and requesting a review from the Twisted dev team.

@adiroiban adiroiban left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi Eric.
Thanks for the fix. Much appreciated

Changes looks good.

I left a few minor comments.

From the interface documentation I see

Call a function when the reactor is running.

If the reactor has not started, the callable will be scheduled to run when it does start. Otherwise, the callable will be invoked immediately.

Comment thread src/twisted/internet/testing.py Resolved
Comment thread src/twisted/newsfragments/12514.bugfix Resolved
Co-authored-by: Adi Roiban <[email protected]>
MadLittleMods added a commit to element-hq/synapse that referenced this pull request Sep 30, 2025
@adiroiban adiroiban changed the title Fix MemoryReactor.callWhenRunning not invoking callbacks if already started #12514 Fix MemoryReactor.callWhenRunning not invoking callbacks if already started Oct 1, 2025
@adiroiban
adiroiban merged commit ffb0ab7 into twisted:trunk Oct 1, 2025
@MadLittleMods

Copy link
Copy Markdown
Contributor Author

Thanks for the review and merge @adiroiban 🙂

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.

3 participants