Skip to content

Use a dispatcher per connection instead of a global one

Andrej Shadura requested to merge use-per-connection-dispatcher into master

Using a single dispatcher never works with decorated functions and context capturing: each time a new connection appears, it overwrites the worker_ping method in the dispatcher.

The idea behind the original code was based on a momentary misunderstanding of how Python’s decorators and scoping works. It was supposed to have added a single entry point into the JSON RPC dispatcher, which would capture the queue context from the enclosing function, handle it and then call the actual worker_ping handler.

Unfortunately, that’s not how 1) decorators, 2) nested functions or 3) scope capturing works in Python. The decorator just takes the function, wraps in and returns it back (under the original name), with the context already enclosed. However, .add_method decorators also adds the newly created function into the global state of dispatcher, overwriting the previously configured handler for worker_ping, resulting in all ping messages going to the handler created while handling the last connection.

The proper solution is, of course, to use a dispatcher per connection.

Merge request reports