-
Notifications
You must be signed in to change notification settings - Fork 19
[crashtracking] improve poll waiting logic #754
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -8,12 +8,13 @@ use super::emitters::emit_crashreport; | |||
| use super::saguard::SaGuard; | ||||
| use crate::crash_info::CrashtrackerMetadata; | ||||
| use crate::shared::configuration::{CrashtrackerConfiguration, CrashtrackerReceiverConfig}; | ||||
| use crate::shared::constants::*; | ||||
| use anyhow::Context; | ||||
| use libc::{ | ||||
| c_void, execve, mmap, sigaltstack, siginfo_t, MAP_ANON, MAP_FAILED, MAP_PRIVATE, PROT_NONE, | ||||
| PROT_READ, PROT_WRITE, SIGSTKSZ, | ||||
| }; | ||||
| use nix::poll::{poll, PollFd, PollFlags}; | ||||
| use libc::{poll, pollfd, POLLHUP}; | ||||
| use nix::sys::signal::{self, SaFlags, SigAction, SigHandler, SigSet}; | ||||
| use nix::sys::socket; | ||||
| use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus}; | ||||
|
|
@@ -22,7 +23,7 @@ use std::ffi::CString; | |||
| use std::fs::{File, OpenOptions}; | ||||
| use std::io::Write; | ||||
| use std::os::unix::{ | ||||
| io::{BorrowedFd, FromRawFd, IntoRawFd, RawFd}, | ||||
| io::{FromRawFd, IntoRawFd, RawFd}, | ||||
| net::UnixStream, | ||||
| }; | ||||
| use std::ptr; | ||||
|
|
@@ -228,20 +229,29 @@ fn run_receiver_child(uds_parent: RawFd, uds_child: RawFd, stderr: RawFd, stdout | |||
| } | ||||
|
|
||||
| fn wait_for_pollhup(target_fd: RawFd, timeout_ms: i32) -> anyhow::Result<bool> { | ||||
| // Need to convert the RawFd into a BorrowedFd to satisfy the PollFd prototype | ||||
| let target_fd = unsafe { BorrowedFd::borrow_raw(target_fd) }; | ||||
| let poll_fd = PollFd::new(&target_fd, PollFlags::POLLHUP); | ||||
|
|
||||
| match poll(&mut [poll_fd], timeout_ms)? { | ||||
| -1 => Err(anyhow::anyhow!("poll failed")), | ||||
| 0 => Ok(false), | ||||
| _ => match poll_fd | ||||
| .revents() | ||||
| .ok_or_else(|| anyhow::anyhow!("No revents found"))? | ||||
| { | ||||
| revents if revents.contains(PollFlags::POLLHUP) => Ok(true), | ||||
| _ => Err(anyhow::anyhow!("poll returned unexpected result")), | ||||
| }, | ||||
| let mut poll_fds = [pollfd { | ||||
| fd: target_fd, | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 - BorrowedFd prefferably should be use in conjuction with OwnedFd. borrow_raw - without any guarantees of FD lifetime is problematic. Probably the safest option would be to dup the fd - and own it within the context of this function. Otherwise the code looks like correct but "C'ish" rust :)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #758 to track |
||||
| events: POLLHUP, | ||||
| revents: 0, | ||||
| }]; | ||||
|
|
||||
| match unsafe { poll(poll_fds.as_mut_ptr(), 1, timeout_ms) } { | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: this should be |
||||
| -1 => Err(anyhow::anyhow!( | ||||
| "poll failed with errno: {}", | ||||
| std::io::Error::last_os_error() | ||||
| )), | ||||
| 0 => Ok(false), // Timeout occurred | ||||
| _ => { | ||||
| let revents = poll_fds[0].revents; | ||||
| if revents & POLLHUP != 0 { | ||||
| Ok(true) // POLLHUP detected | ||||
| } else { | ||||
| Err(anyhow::anyhow!( | ||||
| "poll returned unexpected result: revents = {}", | ||||
| revents | ||||
| )) | ||||
| } | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
||||
|
|
@@ -484,7 +494,10 @@ fn receiver_finish(receiver: Receiver, start_time: Instant, timeout_ms: u32) { | |||
| } | ||||
|
|
||||
| let receiver_pid_as_pid = Pid::from_raw(receiver.receiver_pid); | ||||
| let reaping_allowed_ms = timeout_ms.saturating_sub(start_time.elapsed().as_millis() as u32); | ||||
| let reaping_allowed_ms = std::cmp::min( | ||||
| timeout_ms.saturating_sub(start_time.elapsed().as_millis() as u32), | ||||
| DD_CRASHTRACK_MINIMUM_REAP_TIME_MS, | ||||
| ); | ||||
|
|
||||
| let _ = reap_child_non_blocking(receiver_pid_as_pid, reaping_allowed_ms); | ||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
In sidecar - we send kill and term. When the timeout ends. And it looks that - we're not doing that here either way - so a non 0 timeout will only reduce the incidence of zombies. Not prevent them.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we do that just above? https://github.com/DataDog/libdatadog/blob/main/crashtracker/src/collector/crash_handler.rs#L483 |
||||
| } | ||||
|
|
||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment to explain the meaning of the boolean result