Skip to content

Commit

Permalink
Fixed mirrord-intproxy lingering when executing a non-existent bina…
Browse files Browse the repository at this point in the history
…ry (#2870)

* added kill_on_drop to intproxy command

* Changelog

* Update mirrord/cli/src/execution.rs

Co-authored-by: Gemma <[email protected]>

---------

Co-authored-by: Gemma <[email protected]>
  • Loading branch information
Razz4780 and gememma authored Oct 24, 2024
1 parent 7cb7733 commit 4cec84f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
1 change: 1 addition & 0 deletions changelog.d/2869.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`mirrord-intproxy` no longer lingers forever when the user tries to execute a non-existent binary.
3 changes: 0 additions & 3 deletions mirrord/cli/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,5 @@ pub(crate) async fn container_command(

analytics.set_error(AnalyticsError::BinaryExecuteFailed);

// Kills the intproxy, freeing the agent.
execution_info.stop().await;

Ok(())
}
34 changes: 20 additions & 14 deletions mirrord/cli/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ pub(crate) const INJECTION_ENV_VAR: &str = LINUX_INJECTION_ENV_VAR;
#[cfg(target_os = "macos")]
pub(crate) const INJECTION_ENV_VAR: &str = "DYLD_INSERT_LIBRARIES";

/// Struct for holding the execution information
/// What agent to connect to, what environment variables to set
/// Struct for holding the execution information.
///
/// 1. Environment to set in the user process,
/// 2. Environment to unset in the user process,
/// 3. Path to the patched binary to `exec` into (SIP, only on macOS).
#[derive(Debug, Serialize)]
pub(crate) struct MirrordExecution {
pub environment: HashMap<String, String>,
Expand Down Expand Up @@ -146,8 +149,19 @@ where
}

impl MirrordExecution {
/// Starts the internal proxy (`intproxy`), and mirrord-layer, even if a bogus binary
/// was passed by the user.
/// Makes agent connection and starts the internal proxy child process.
///
/// # Internal proxy
///
/// The internal proxy will be killed as soon as this struct is dropped.
/// It **does not** happen when you `exec` into user binary, because Rust destructors are not
/// run. The whole process is instantly replaced by the OS.
///
/// Therefore, everything should work fine when you create [`MirrordExecution`] with this
/// function and then either:
/// 1. Drop this struct when an error occurs,
/// 2. Successfully `exec`,
/// 3. Wait for intproxy exit with [`MirrordExecution::wait`].
#[tracing::instrument(level = Level::TRACE, skip_all)]
pub(crate) async fn start<P>(
config: &LayerConfig,
Expand Down Expand Up @@ -219,7 +233,8 @@ impl MirrordExecution {
.arg("intproxy")
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.stdin(std::process::Stdio::null());
.stdin(std::process::Stdio::null())
.kill_on_drop(true);

proxy_command.env(
AGENT_CONNECT_INFO_ENV_KEY,
Expand Down Expand Up @@ -537,13 +552,4 @@ impl MirrordExecution {

Ok(())
}

/// Kills the child process, stopping the internal proxy, and completing the agent.
///
/// Used when mirrord execution fails inside `execvp`.
pub async fn stop(self) {
let Self { mut child, .. } = self;

let _ = child.start_kill();
}
}
3 changes: 0 additions & 3 deletions mirrord/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ where
error!("Couldn't execute {:?}", errno);
analytics.set_error(AnalyticsError::BinaryExecuteFailed);

// Kills the intproxy, freeing the agent.
execution_info.stop().await;

#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
if errno == Errno::from_raw(86) {
// "Bad CPU type in executable"
Expand Down

0 comments on commit 4cec84f

Please sign in to comment.