Skip to content

Lost write coroutines from asyncio.ensure_future #609

@meymchen

Description

@meymchen

Component

pygls/protocol/json_rpc.py

Summary

When sending data over an async writer, the result of writer.write() is sometimes awaitable. The code calls asyncio.ensure_future(res) and immediately drops the returned Task. Exceptions are silently lost and the task cannot be cancelled during shutdown.

Steps to Reproduce

  1. Send a large payload over an async transport under backpressure.
  2. The write task is created but not tracked.

Expected Behavior

Pending writes should be tracked so they can be awaited and errors caught.

Actual Behavior

Write tasks are fire-and-forget.

Affected Code (pygls/protocol/json_rpc.py, ~L541-543)

res = self.writer.write(data.encode(self.CHARSET))
if inspect.isawaitable(res):
    asyncio.ensure_future(res)

Proposed Fix

Track the task in a set and discard it when done:

if inspect.isawaitable(res):
    task = asyncio.ensure_future(res)
    self._pending_writes.add(task)
    task.add_done_callback(self._pending_writes.discard)

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