Component
pygls/protocol/json_rpc.py
Summary
handle_message copies the entire context variables mapping for every single JSON-RPC message. High-frequency notifications such as textDocument/didChange do not need this isolation because they have no message ID and cannot be correlated.
Steps to Reproduce
- Rapidly type in an editor connected to a pygls server.
- Observe CPU overhead from
contextvars.copy_context in profiles.
Expected Behavior
Context isolation should only be performed for requests that need ID correlation.
Actual Behavior
Every notification pays the copy cost.
Affected Code (pygls/protocol/json_rpc.py, ~L501)
ctx = contextvars.copy_context()
Proposed Fix
Only create the copied context inside the RPCRequest branch; call notification and response handlers directly.
Component
pygls/protocol/json_rpc.py
Summary
handle_messagecopies the entire context variables mapping for every single JSON-RPC message. High-frequency notifications such astextDocument/didChangedo not need this isolation because they have no message ID and cannot be correlated.Steps to Reproduce
contextvars.copy_contextin profiles.Expected Behavior
Context isolation should only be performed for requests that need ID correlation.
Actual Behavior
Every notification pays the copy cost.
Affected Code (
pygls/protocol/json_rpc.py, ~L501)Proposed Fix
Only create the copied context inside the
RPCRequestbranch; call notification and response handlers directly.