mbr / tinyrpc

A compact, modular transport and protocol agnostic RPC library. Does jsonrpc v2.
https://tinyrpc.readthedocs.org
MIT License
156 stars 53 forks source link

Multiple JSONRPCProtocol generators may share the same unique ID generator #96

Closed ntamas closed 1 year ago

ntamas commented 2 years ago

Recent changes introduced to JSONRPCProtocol (and other classes that allow the user to provide a custom ID generator) introduced a situation where multiple JSONRPCProtocol instances constructed with default arguments (i.e. no explicitly provided ID generator) share the same global ID generator. As a consequence, unit tests employing an unparameterized JSONRPCProtocol instance in downstream projects may no longer assume that the IDs of a JSONRPCProtocol instance start from 1. Furthermore, this leads to unpredictable results if unit tests are run in parallel.

The solution would be to change the constructor of JSONRPCProtocol (and other protocol classes using the same solution) as follows:

def __init__(self, id_generator: Optional[Generator[object, None, None]] = None, *args, **kwds):
    [...]
    self._id_generator = id_generator or default_id_generator()

This creates a per-instance ID generator so separately constructed JSONRPCProtocol instances no longer have a dependency between them.

ntamas commented 2 years ago

Submitted PR #97 as a fix for this issue.