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

StdoutLock not released on drop. #640

Open
Emm54321 opened this issue Dec 21, 2019 · 10 comments
Open

StdoutLock not released on drop. #640

Emm54321 opened this issue Dec 21, 2019 · 10 comments
Labels
bug Something isn't working

Comments

@Emm54321
Copy link

This causes a deadlock with 1.4.0 :

async fn f() {
    {
        let _ = async_std::io::stdout().lock().await;
    }
    {
        let _ = async_std::io::stdout().lock().await;
    }
}
@Noah-Kennedy
Copy link
Contributor

I'm noticing something similar with some mutexes at the moment. I'll see if I can post some sample code tomorrow to reproduce my issue. It looks like mutexes don't relinquish locks, even when I make the calls to drop() explicitly.

@tmiasko
Copy link

tmiasko commented Jan 2, 2020

async_std::io::StdoutLock is a thin wrapper around similar struct from std
with an additional unasfe impl of Send trait. The mutex used internally in
std::io::StdoutLock can be unlocked only from the same thread.

Thus in scenario above unlocking will fail (silently or with panic, depending
on debug assertions in std), hence a deadlock.

@Noah-Kennedy
Copy link
Contributor

Noah-Kennedy commented Jan 8, 2020

@tmiasko Does anything similar apply to Mutex, or does this just affect StdoutLock?

@yoshuawuyts
Copy link
Contributor

The mutex used internally in std::io::StdoutLock can be unlocked only from the same thread.

@tmiasko could you perhaps share more on this?

@yoshuawuyts yoshuawuyts added the bug Something isn't working label Jan 10, 2020
@tmiasko
Copy link

tmiasko commented Jan 10, 2020

Typically a mutex can be unlocked only by the owner, i.e., thread that locked it. The std::io::StdoutLock is no different since on POSIX systems it uses pthread mutex with such a requirement. It is also marked as !Send to enforce that.

@Noah-Kennedy
Copy link
Contributor

Interesting. This could be pretty confusing and counter-intuitive behavior for many people. Are there any plans to address this sort of behavior within async-std? I'm assuming fixing this would require changes to the task scheduler, at the very least.

@yoshuawuyts
Copy link
Contributor

@Noah-Kennedy from talking with @stjepang in a call yesterday, we're considering maybe dropping this range of APIs all together. The new scheduler outlined in #631 would help with any (potentially) slow IO.

There are probably ways of writing true async stdio APIs, but to make them coordinate with std seemingly requires a lot of effort, and probably buy-in from std itself too. #631 might just be the best option we have for the foreseeable future.

@Noah-Kennedy
Copy link
Contributor

@yoshuawuyts which APIs would be dropped? stdout() and Mutex?

@Noah-Kennedy
Copy link
Contributor

I could see the new scheduler allowing those to be removed, with people just using the Mutex and blocking IO in std, especially where said IO involves a lock.

@yoshuawuyts
Copy link
Contributor

@Noah-Kennedy just stdout/stdin/stderr; our async mutex impl actually has distinct benefits compared to always blocking threads.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants