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

Signal refactor #2835

Merged
merged 9 commits into from Sep 22, 2020
Merged

Signal refactor #2835

merged 9 commits into from Sep 22, 2020

Conversation

ipetkov
Copy link
Member

@ipetkov ipetkov commented Sep 14, 2020

This refactors how Unix signals are dispatched:

  • A new signal driver is introduced to be the central receiver of wake events coming from the OS and dispatching the events to any Signal listeners
  • This will lead to fewer spurious wakups since tasks blocked on a Signal instance will only get woken up when their respective signal is received, rather than always being woken up whenever any signal is received
  • This also leads to using fewer file descriptors. With this change a single file descriptor is needed per driver/runtime rather than requiring duplicating a file descriptor per Signal instance

There is an open question regarding how a runtime should be configured given that we're introducing the concept of dependent drivers, in this case signal depending on io (the dependency was alway there implicitly). For example, using the process module on Unix systems requires the signal feature, and the io feature transitively, but on Windows neither of the modules are required. Should we require that a runtime is explicitly built with all required features, or should turning on a feature transitively enable dependent features?

  • on one hand, an explicit configuration avoids issues with implicitly relying on a transitive feature such that if internal refactoring leads to that feature no longer being required, then such code would break
  • on the other hand, it potentially leaks out details (e.g. the process -> signal -> io dependency), and subsequent internal refactors could lead to older code relying on more features than they need (e.g. process stops requiring signal)

* Merge the runtime::{io, time} modules into runtime::driver
* Create a concret Driver type which encompases the conditionally
  compiled `io` and `time` drivers
* This avoids having other code directly refer to
  `runtime::time::Driver` etc. so that other drivers can be added
  transparently
* This offers everal benefits:
 - fewer spurious wakeups: tasks blocked on a `Signal` instance will
   only get woken up when their respective signal is received rather
   than on *any* signal
 - using fewer file descriptors: we now need one file descriptor *per
   driver/runtime* rather than one per `Signal` instance for receiving
   wakeups
* This avoids lazy registration and synchronization during every turn
@ipetkov ipetkov added A-tokio Area: The main tokio crate M-runtime Module: tokio/runtime M-signal Module: tokio/signal labels Sep 14, 2020
@ipetkov ipetkov requested a review from a team September 14, 2020 00:08
@ipetkov
Copy link
Member Author

ipetkov commented Sep 14, 2020

FWIW I did run the benchmarks before and after the changes on my under-powered machine, but the results would vary wildly in between consecutive runs so I did not bother publishing them here, but happy to if someone would like to see them.

@Darksonn Darksonn added the C-enhancement Category: A PR with an enhancement or bugfix. label Sep 14, 2020
@carllerche
Copy link
Member

Thanks for getting this done. I did a quick skim, but this is going to take more time to go over closer :) I will look asap.

cc/ @seanmonstar this relates to your I/O driver work as well.

Copy link
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 Thanks for making this happen.

@ipetkov
Copy link
Member Author

ipetkov commented Sep 22, 2020

Thanks for the reviews!

Just wanted to mention that @carllerche and I discussed this in the discord and we decided to omit the enable_signal method on the runtime builder, and enable the signal driver if io is enabled (we already have a compile time flag to disable signal for those that do not need it), so I will update

@carllerche carllerche merged commit 7ae5b7b into tokio-rs:master Sep 22, 2020
@ipetkov ipetkov deleted the signal-refactor branch September 22, 2020 22:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-enhancement Category: A PR with an enhancement or bugfix. M-runtime Module: tokio/runtime M-signal Module: tokio/signal
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants