Skip to content
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

fix: don't create static variables containing a tokio::runtime::Handle #38

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

Conversation

bonsairobo
Copy link
Contributor

@bonsairobo bonsairobo commented Dec 6, 2024

The Bug

There was a bug in the approach implemented in #30.

The issue could manifest as a panic with message:

"A Tokio 1.x context was found, but it is being shutdown."

This would occur in the following scenario:

  1. .compat() was used within a tokio context other than the fallback runtime.
  2. That runtime was shutdown.
  3. .compat() was used again.

The root cause being that we had a Handle stored in a static global. Handles are weak by design, and cannot prevent the runtime from shutting down.

The Fix

I reverted most of the changes in #30 and instead added a get_runtime_handle function. This function attempts to acquire an existing runtime handle with try_current before creating a fallback runtime. This should be safe because try_current will only succeed within the scope of a tokio runtime context, and it cannot outlive that scope.

There was a bug in the approach implemented in smol-rs#30.

The issue could manifest as a panic with message:

"A Tokio 1.x context was found, but it is being shutdown."

This would occur in the following scenario:

1. `.compat()` was used within a tokio context other than the fallback runtime.
2. That runtime was shutdown.
3. `.compat()` was used again.

The root cause being that we had a `Handle` stored in a `static` global.
`Handle`s are weak by design, and cannot prevent the runtime from shutting down.

I reverted most of the changes in smol-rs#30 and instead added a `get_runtime_handle` function.
This function attempts to acquire an existing runtime handle with `try_current` before creating a fallback runtime.
This should be safe because `try_current` will only succeed within the scope of a tokio runtime context,
and it cannot outlive that scope.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant