-
Notifications
You must be signed in to change notification settings - Fork 392
Wrap the Rust HTTP client with make_deferred_yieldable
#18903
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
base: develop
Are you sure you want to change the base?
Wrap the Rust HTTP client with make_deferred_yieldable
#18903
Conversation
So downstream usage doesn't need to use `PreserveLoggingContext()` or `make_deferred_yieldable` Spawning from #18870 and #18357 (comment)
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.
OK to merge or otherwise would be happy to see the wrapperless approach if you have the energy for it
…ve-preserve-logging-context
…ve-preserve-logging-context
…ve-preserve-logging-context
…ve-preserve-logging-context
See #18903 (comment) --- Related to twisted/twisted#12514
/// Given a deferred, make it follow the Synapse logcontext rules | ||
fn make_deferred_yieldable<'py>( | ||
py: Python<'py>, | ||
deferred: &Bound<'py, PyAny>, | ||
) -> Bound<'py, PyAny> { | ||
let make_deferred_yieldable = MAKE_DEFERRED_YIELDABLE.get_or_init(|| { | ||
let sys = PyModule::import(py, "synapse.logging.context").unwrap(); | ||
let func = sys.getattr("make_deferred_yieldable").unwrap().unbind(); | ||
func | ||
}); | ||
|
||
make_deferred_yieldable | ||
.call1(py, (deferred,)) | ||
.unwrap() | ||
.extract(py) | ||
.unwrap() | ||
} |
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 is based on @reivilibre suggestion in #18903 (comment)
But please double-check that I didn't fumble the lifetimes, etc trying to kludge things together.
url=self.server.endpoint, | ||
response_limit=1 * 1024 * 1024, | ||
) | ||
self._check_current_logcontext("competing") |
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.
I've confirmed this test fails without the changes to rust/src/http_client.rs
To reproduce:
- Checkout this PR:
git checkout madlittlemods/18357-rust-http-client-remove-preserve-logging-context
- Use the old version of the Rust HTTP Client:
git checkout develop -- rust/src/http_client.rs
- Rebuild Rust:
poetry install --extras all
- Run the test:
SYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.synapse_rust.test_http_client.HttpClientTestCase.test_logging_context
SYNAPSE_TEST_LOG_LEVEL=INFO poetry run trial tests.synapse_rust.test_http_client.HttpClientTestCase.test_logging_context
tests.synapse_rust.test_http_client
HttpClientTestCase
test_logging_context ... [ERROR]
===============================================================================
[ERROR]
Traceback (most recent call last):
File "/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/python3.13/site-packages/twisted/internet/defer.py", line 1857, in _inlineCallbacks
result = context.run(gen.send, result)
File "synapse/tests/synapse_rust/test_http_client.py", line 196, in do_request
self._check_current_logcontext("competing")
File "synapse/tests/synapse_rust/test_http_client.py", line 151, in _check_current_logcontext
self.assertEqual(
File "/virtualenvs/matrix-synapse-xCtC9ulO-py3.13/lib/python3.13/site-packages/twisted/trial/_synctest.py", line 444, in assertEqual
super().assertEqual(first, second, msg)
File "/usr/lib/python3.13/unittest/case.py", line 907, in assertEqual
assertion_func(first, second, msg=msg)
File "/usr/lib/python3.13/unittest/case.py", line 1273, in assertMultiLineEqual
self.fail(self._formatMessage(msg, standardMsg))
twisted.trial.unittest.FailTest: 'sentinel' != 'competing'
- sentinel
+ competing
: Expected LoggingContext(competing) but saw sentinel
tests.synapse_rust.test_http_client.HttpClientTestCase.test_logging_context
-------------------------------------------------------------------------------
Ran 1 tests in 0.251s
FAILED (errors=1)
self.get_success(self.till_deferred_has_result(do_request())) | ||
self.assertEqual(self.server.calls, 1) | ||
|
||
async def test_logging_context(self) -> None: |
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 test is based off the tests we have in tests/util/test_logcontext.py
""" | ||
The returned deferreds follow Synapse logcontext rules. | ||
""" |
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.
Perhaps we should just remove this as we should just assume that everything in Synapse does follow the Synapse logcontext rules unless otherwise stated.
For example:
synapse/synapse/logging/context.py
Lines 853 to 856 in d1c96ee
Returns: | |
Deferred which returns the result of func, or `None` if func raises. | |
Note that the returned Deferred does not follow the synapse logcontext | |
rules. |
…ve-preserve-logging-context
# XXX: We must create the Rust HTTP client before we call `reactor.run()` below. | ||
# Twisted's `MemoryReactor` doesn't invoke `callWhenRunning` callbacks if it's | ||
# already running and we rely on that to start the Tokio thread pool in Rust. In | ||
# the future, this may not matter, see https://github.com/twisted/twisted/pull/12514 |
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.
Created a Twisted PR to fix this flaw in the MemoryReactor
-> twisted/twisted#12514
Seems to have buy-in (approved) so hopefully we don't need to deal with this in the future.
Wrap the Rust HTTP client with
make_deferred_yieldable
so downstream usage doesn't need to usePreserveLoggingContext()
ormake_deferred_yieldable
.Spawning from wanting to remove
PreserveLoggingContext()
from the codebase and thinking that we shouldn't have to pollute all downstream usage withPreserveLoggingContext()
ormake_deferred_yieldable
Part of #18905 (Remove
sentinel
logcontext where we log in Synapse)Dev notes
Tokio runtime is not running
-> #18903 (comment)MemoryReactor.callWhenRunning
not invoking callbacks if already started twisted/twisted#12514Pull Request Checklist
EventStore
toEventWorkerStore
.".code blocks
.