tokio icon indicating copy to clipboard operation
tokio copied to clipboard

Identify if code is within blocking or async context

Open swarnimarun opened this issue 4 months ago • 2 comments

Is your feature request related to a problem? Please describe. Currently I ideally want to write libraries with assets at runtime that crash the app if used within async context since they are blocking apis, so I ideally want a way to check with task try_id and then check if the task exists then is it on a blocking thread or async.

Describe the solution you'd like I tried adding some metadata for the task that seems to work, but not sure if it's a good solution. For which I made a minor fork.

Describe alternatives you've considered Nothing else meaningfully different, but if a workaround exists that already works we should document it.

Additional context I really would like some way to check if the thread we are running on would block a thread that expects to be "yieldable" or "async". I honestly don't feel very strongly about any of my ideas, and if someone has a good solution or workaround that works today that might be good enough for me.

swarnimarun avatar Dec 10 '25 08:12 swarnimarun

This looks like a very rare use case. The tokio::task::try_id only tell us if we are in a tokio task which might be an async task, or a task spawned by tokio::task::spawn_blocking. Thus, it is possible for us to get a real task id even if we are in a tokio::task::spawn_blocking.

This use case looks a bit weird, could you share more context about it? We may come up with another solution with more context.

ADD-SP avatar Dec 10 '25 11:12 ADD-SP

This looks like a very rare use case. The tokio::task::try_id only tell us if we are in a tokio task which might be an async task, or a task spawned by tokio::task::spawn_blocking. Thus, it is possible for us to get a real task id even if we are in a tokio::task::spawn_blocking.

I know. The point is I would like for a way to know/crash if some "possibly" blocking apis are running on "async" runtime threads, aka, them blocking could cause the async runtime to freeze up in weird ways.

So imagine I have been trying to create APIs where when you run them in debug mode we panic if you call the APIs from anything that is on an async thread.

This is useful as sometimes you have blocking apis that aren't really blocking on most systems but only blocking under very specific edge cases hard to reproduce on CI systems, and you would like to block them from being used in-correctly by mistake, by writing aggressive tests. Instead of doing massive re-write of the entire API immediately, to encode this all in types or exposing a non-blocking API.

swarnimarun avatar Dec 10 '25 18:12 swarnimarun

I don't remember we have such recommendation, but is tokio::runtime::Handle::try_current helpful?

ADD-SP avatar Dec 11 '25 15:12 ADD-SP

try_current specifically is not enough since we can be inside a spawn_blocking created thread where it will succeed.

It works only to check if we are in non tokio thread.

swarnimarun avatar Dec 11 '25 17:12 swarnimarun