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

Fix potential deadlock when saving to file after logging at the end of a Python program #3920

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions rerun_py/src/python_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ fn connect(
addr: Option<String>,
flush_timeout_sec: Option<f32>,
recording: Option<&PyRecordingStream>,
py: Python<'_>,
) -> PyResult<()> {
let addr = if let Some(addr) = addr {
addr.parse()?
Expand All @@ -483,40 +484,53 @@ fn connect(

let flush_timeout = flush_timeout_sec.map(std::time::Duration::from_secs_f32);

if let Some(recording) = recording {
// If the user passed in a recording, use it
recording.connect(addr, flush_timeout);
} else {
// Otherwise, connect both global defaults
if let Some(recording) = get_data_recording(None) {
// The call to connect may internally flush.
// Release the GIL in case any flushing behavior needs to cleanup a python object.
py.allow_threads(|| {
if let Some(recording) = recording {
// If the user passed in a recording, use it
recording.connect(addr, flush_timeout);
};
if let Some(blueprint) = get_blueprint_recording(None) {
blueprint.connect(addr, flush_timeout);
};
}
} else {
// Otherwise, connect both global defaults
if let Some(recording) = get_data_recording(None) {
recording.connect(addr, flush_timeout);
};
if let Some(blueprint) = get_blueprint_recording(None) {
blueprint.connect(addr, flush_timeout);
};
}
});

Ok(())
}

#[pyfunction]
#[pyo3(signature = (path, recording = None))]
fn save(path: &str, recording: Option<&PyRecordingStream>) -> PyResult<()> {
fn save(path: &str, recording: Option<&PyRecordingStream>, py: Python<'_>) -> PyResult<()> {
let Some(recording) = get_data_recording(recording) else {
return Ok(());
};

recording
.save(path)
.map_err(|err| PyRuntimeError::new_err(err.to_string()))
// The call to save may internally flush.
// Release the GIL in case any flushing behavior needs to cleanup a python object.
py.allow_threads(|| {
recording
.save(path)
.map_err(|err| PyRuntimeError::new_err(err.to_string()))
})
}

/// Create an in-memory rrd file
#[pyfunction]
#[pyo3(signature = (recording = None))]
fn memory_recording(recording: Option<&PyRecordingStream>) -> Option<PyMemorySinkStorage> {
fn memory_recording(
recording: Option<&PyRecordingStream>,
py: Python<'_>,
) -> Option<PyMemorySinkStorage> {
get_data_recording(recording).map(|rec| {
let inner = rec.memory();
// The call to memory may internally flush.
// Release the GIL in case any flushing behavior needs to cleanup a python object.
let inner = py.allow_threads(|| rec.memory());
PyMemorySinkStorage { rec: rec.0, inner }
})
}
Expand Down
Loading