Skip to content

contextvars.copy_context() is called for every message including notifications #610

@meymchen

Description

@meymchen

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

  1. Rapidly type in an editor connected to a pygls server.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions