Skip to content

Await async cancellation cleanup#42

Closed
Suke0811 wants to merge 1 commit intomainfrom
codex/await-task-cancellation-in-stop_spinning
Closed

Await async cancellation cleanup#42
Suke0811 wants to merge 1 commit intomainfrom
codex/await-task-cancellation-in-stop_spinning

Conversation

@Suke0811
Copy link
Owner

Summary

  • add awaited async shutdown path that cancels tasks and closes owned loops safely
  • ensure async spin context manager awaits task cancellation during exit
  • add coverage for awaited cancellation and context cleanup

Testing

  • PYTHONPATH=. pytest tests/test_ratecontrol.py::test_stop_spinning_async_task_cancel tests/test_ratecontrol.py::test_async_context_manager_waits_for_cleanup

Codex Task

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +496 to +499
loop = asyncio.get_running_loop()
except RuntimeError:
asyncio.run(self.stop_spinning_async())
else:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Stop async spins on wrong loop

stop_spinning now runs stop_spinning_async() via asyncio.run when no loop is running (lines 496-499), but stop_spinning_async awaits self._task, which remains bound to the loop that created it. When the spin is running on an owned/background loop, calling stop_spinning() from sync code now raises RuntimeError: ... attached to a different loop and leaves the task running. The cleanup coroutine needs to be scheduled on the task’s loop (e.g., via self._own_loop/run_coroutine_threadsafe) instead of a fresh loop.

Useful? React with 👍 / 👎.

@Suke0811 Suke0811 closed this Nov 25, 2025
@Suke0811 Suke0811 deleted the codex/await-task-cancellation-in-stop_spinning branch November 25, 2025 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant