Skip to content

ContextVar with tracing breaks responses #19004

@erikjohnston

Description

@erikjohnston

When upgrading jki.re 19fe3f0..c284d8c (1.137.0 -> 1.139.0) I started noticing that some requests had their responses cut short and the following exception appearing in the logs:

2025-10-01 13:19:49,431 - twisted - 278 - CRITICAL - sentinel - 
Traceback (most recent call last):
  File "/home/erikj/synapse/src/synapse/synapse/http/server.py", line 804, in resumeProducing
    data = next(self._iterator)
           ^^^^^^^^^^^^^^^^^^^^
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/erikj/.virtualenvs/synapse311/lib/python3.11/site-packages/twisted/internet/asyncioreactor.py", line 138, in _readOrWrite
    why = method()
          ^^^^^^^^
  File "/home/erikj/.virtualenvs/synapse311/lib/python3.11/site-packages/twisted/internet/abstract.py", line 298, in doWrite
    self.producer.resumeProducing()
  File "/home/erikj/.virtualenvs/synapse311/lib/python3.11/site-packages/twisted/web/http.py", line 2953, in resumeProducing
    self._requestProducer.resumeProducing()
  File "/home/erikj/synapse/src/synapse/synapse/http/server.py", line 814, in resumeProducing
    self.stopProducing()
  File "/home/erikj/synapse/src/synapse/synapse/http/server.py", line 822, in stopProducing
    self.tracing_scope.__exit__(None, None, None)
  File "/home/erikj/.virtualenvs/synapse311/lib/python3.11/site-packages/opentracing/scope.py", line 84, in __exit__
    self.close()
  File "/home/erikj/.virtualenvs/synapse311/lib/python3.11/site-packages/opentracing/scope_managers/contextvars.py", line 101, in close
    _SCOPE.reset(self._token)
ValueError: <Token var=<ContextVar name='scope' at 0x7f90b631a190> at 0x7f90b284d150> was created in a different Context

Note that jki.re uses asyncioreactor

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions