Git Product home page Git Product logo

Comments (5)

notgull avatar notgull commented on July 30, 2024

It appears that tokio uses a socket pair as well.

The reason why is because its impossible to use an async channel in a signal handler, at least in the general case. Waker can run general user code, and general user code can be non-signal safe.

from async-signal.

SteveLauC avatar SteveLauC commented on July 30, 2024

Hi, thanks for your reply!

It appears that tokio uses a socket pair as well.

I just took a closer look at Tokio's implementation, it seems more complex than I originally thought. Tokio uses both mio::net::UnixStream and tokio::sync::watch, every signal has an atomic flag indicating whether this signal has been sent (i.e., pending state), the the signal handler it registers does 2 things:

  1. Mark the atomic flag so that the signal is pending (NOTE: it is just pending, the corresponding task won't be notified for now)
  2. Write one byte to the tx end of the UnixStream, so that the Driver can be woke up

Once the Driver is woken, it will broadcast the pending signals, i.e., scan all the atomic flags to check if there is any signal that is in the pending state, if so, send a message (()) to the tx end of the corresponding tokio::sync::watch::Sender<()>. Currently, I don't quite understand the purpose behind this 2-layer design.


The reason why is because its impossible to use an async channel in a signal handler, at least in the general case.

Do you mean we cannot call an async function in the sync signal handler? If tx.send() is not async, I guess it would work?

Waker can run general user code, and general user code can be non-signal safe.

Signal-safety is hard, and actually I am struggling with it. Context: I am trying to build the signal futures provided by tokio::signal using event-listener so that I can use them in monoio or other non-Tokio runtimes (Since Tokio signal futures use its io::Driver, it won't work on other runtimes.), looks like there is still something that is not signal-safe in my code, I can get deadlocks sometimes when running unit tests.

from async-signal.

notgull avatar notgull commented on July 30, 2024

Do you mean we cannot call an async function in the sync signal handler? If tx.send() is not async, I guess it would work?

The set of functions that can be called inside of a signal handler is quite small. See this manpage for a list of them.

Signal-safety is hard, and actually I am struggling with it. Context: I am trying to build the signal futures provided by tokio::signal using event-listener so that I can use them in monoio or other non-Tokio runtimes (Since Tokio signal futures use its io::Driver, it won't work on other runtimes.), looks like there is still something that is not signal-safe in my code, I can get deadlocks sometimes when running unit tests.

It should be impossible from the public API exposed by this crate to call any functions in a signal handler that are signal-unsafe. This could be an issue with event-listener, I would appreciate it if you opened an issue on that repository.

from async-signal.

SteveLauC avatar SteveLauC commented on July 30, 2024

The reason why is because its impossible to use an async channel in a signal handler, at least in the general case.

The set of functions that can be called inside of a signal handler is quite small. See this manpage for a list of them.

I guess you are saying that an async channel may allocate? And that's why this crate uses socket and ctrlc uses pipe.

Currently, I don't quite understand the purpose behind this 2-layer design

And, I kinda think this is also the reason behind Tokio's design.

from async-signal.

SteveLauC avatar SteveLauC commented on July 30, 2024

Close as the question has been answered.

from async-signal.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.