Skip to content

Handle closing connection when event loop is closed #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alisaifee
Copy link

@alisaifee alisaifee commented Mar 14, 2025

Pull Request check-list

  • Do tests and lints pass with this change?
  • Do the CI tests pass with this change (enable it first in your forked repo and wait for the github action build to finish)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Is there an example added to the examples folder (if applicable)?

Description of change

Improve the connection cleanup logic in AbstractConnection by adding a try-except block around the .close() call on the writer to protect against scenarios where the event loop is already closed.

Given an example use case:

import asyncio
import valkey.asyncio

client = valkey.asyncio.Redis.from_url("redis://localhost:7379")
asyncio.run(client.ping())

The following traceback is noted after the process exists:

Exception ignored in: <function AbstractConnection.__del__ at 0x10519e340>
Traceback (most recent call last):
  File "<...>/valkey-py/valkey/asyncio/connection.py", line 243, in __del__
  File "<...>/valkey-py/valkey/asyncio/connection.py", line 250, in _close
  File "<...>/python/3.12.0/lib/python3.12/asyncio/streams.py", line 343, in close
  File "<...>/python/3.12.0/lib/python3.12/asyncio/selector_events.py", line 1206, in close
  File "<...>/python/3.12.0/lib/python3.12/asyncio/selector_events.py", line 871, in close
  File "<...>/python/3.12.0/lib/python3.12/asyncio/base_events.py", line 772, in call_soon
  File "<...>/python/3.12.0/lib/python3.12/asyncio/base_events.py", line 519, in _check_closed
RuntimeError: Event loop is closed

@alisaifee alisaifee force-pushed the handle-on-exit-traceback branch from e6192c2 to 52100a7 Compare March 14, 2025 20:14
@alisaifee alisaifee marked this pull request as ready for review March 14, 2025 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant