Skip to content

Commit

Permalink
Use select! instead of spawn and abort in pipe_signals
Browse files Browse the repository at this point in the history
  • Loading branch information
nbdd0121 committed Mar 4, 2024
1 parent 7ca5bef commit ce537da
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
23 changes: 13 additions & 10 deletions src/docker/container.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::pin::pin;
use std::time::Duration;

use super::{IoStream, IoStreamSource};
Expand Down Expand Up @@ -240,29 +241,31 @@ impl Container {

pub async fn pipe_signals(&self) -> JoinHandle<Result<()>> {
let container = self.clone();
let handle = spawn(async move {
let stream = tokio_stream::empty()
.merge(signal_stream(SignalKind::alarm()))
let signal_handler = async move {
let mut stream = pin!(signal_stream(SignalKind::alarm())
.merge(signal_stream(SignalKind::hangup()))
.merge(signal_stream(SignalKind::interrupt()))
.merge(signal_stream(SignalKind::quit()))
.merge(signal_stream(SignalKind::terminate()))
.merge(signal_stream(SignalKind::user_defined1()))
.merge(signal_stream(SignalKind::user_defined2()));
.merge(signal_stream(SignalKind::user_defined2())));

tokio::pin!(stream);
while let Some(signal) = stream.next().await {
container.kill(signal?.as_raw_value()).await?;
}

Err::<(), Error>(anyhow!("Failed to listen for signals"))
});
Err::<_, Error>(anyhow!("Failed to listen for signals"))
};

let container = self.clone();
let wait_for_exit = async move { container.wait().await };

spawn(async move {
let _ = container.wait().await;
handle.abort();
Ok::<(), Error>(())
tokio::select! {
result = signal_handler => result,
result = wait_for_exit => result,
}?;
Ok(())
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/hotplug/pluggable_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl PluggableDevice {
}

pub fn syspath(&self) -> &Path {
&self.device.syspath()
self.device.syspath()
}

pub fn devnum(&self) -> (u32, u32) {
Expand Down
4 changes: 2 additions & 2 deletions src/hotplug/udev_streams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ pub enum UdevEvent {
pub fn enumerate(hub_path: PathBuf) -> impl tokio_stream::Stream<Item = Result<PluggableDevice>> {
try_stream! {
let mut enumerator = Enumerator::new()?;
let mut devices = enumerator
let devices = enumerator
.scan_devices()?
.filter(|device| device.syspath().starts_with(&hub_path))
.filter_map(|device| PluggableDevice::from_device(&device));

while let Some(device) = devices.next() {
for device in devices {
yield device;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async fn run(param: cli::Run, verbosity: Verbosity<InfoLevel>) -> Result<u8> {

let docker = Docker::connect_with_defaults()?;
let container = docker.run(param.docker_args).await?;
let _ = container.pipe_signals();
drop(container.pipe_signals());

let hub_path = param.root_device.hub()?.syspath().to_owned();
let hotplug_stream = run_hotplug(
Expand Down

0 comments on commit ce537da

Please sign in to comment.