1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2 From: Mike Hommey <mh+mozilla@glandium.org>
3 Date: Sat, 16 Oct 2021 01:19:56 +0000
4 Subject: [PATCH] Bug 1735905 - Upgrade cubeb-pulse to fix a race condition
5 that can lead to shutdown deadlock. r=kinetik
7 Differential Revision: https://phabricator.services.mozilla.com/D128657
9 .cargo/config.in | 2 +-
11 .../rust/cubeb-pulse/.cargo-checksum.json | 2 +-
12 .../rust/cubeb-pulse/src/backend/stream.rs | 33 +++++++++++--------
13 toolkit/library/rust/shared/Cargo.toml | 2 +-
14 5 files changed, 25 insertions(+), 20 deletions(-)
16 diff --git a/.cargo/config.in b/.cargo/config.in
17 index dce119e25da1..6b9752c9fa1c 100644
18 --- a/.cargo/config.in
19 +++ b/.cargo/config.in
20 @@ -25,7 +25,7 @@ rev = "fd02134161505f978e132114cbabdac057ce3b71"
21 [source."https://github.com/mozilla/cubeb-pulse-rs"]
22 git = "https://github.com/mozilla/cubeb-pulse-rs"
23 replace-with = "vendored-sources"
24 -rev = "e9e55a4529642da99e64452467ecaef9f7753531"
25 +rev = "9695281319fcb3e40db6a32cc0661548d6192f4d"
27 [source."https://github.com/mozilla/cubeb-coreaudio-rs"]
28 git = "https://github.com/mozilla/cubeb-coreaudio-rs"
29 diff --git a/Cargo.lock b/Cargo.lock
30 index ca2466600d4e..cc7a65e08715 100644
33 @@ -1059,7 +1059,7 @@ dependencies = [
37 -source = "git+https://github.com/mozilla/cubeb-pulse-rs?rev=e9e55a4529642da99e64452467ecaef9f7753531#e9e55a4529642da99e64452467ecaef9f7753531"
38 +source = "git+https://github.com/mozilla/cubeb-pulse-rs?rev=9695281319fcb3e40db6a32cc0661548d6192f4d#9695281319fcb3e40db6a32cc0661548d6192f4d"
42 @@ -3935,16 +3935,16 @@ dependencies = [
46 -source = "git+https://github.com/mozilla/cubeb-pulse-rs?rev=e9e55a4529642da99e64452467ecaef9f7753531#e9e55a4529642da99e64452467ecaef9f7753531"
47 +source = "git+https://github.com/mozilla/cubeb-pulse-rs?rev=9695281319fcb3e40db6a32cc0661548d6192f4d#9695281319fcb3e40db6a32cc0661548d6192f4d"
56 -source = "git+https://github.com/mozilla/cubeb-pulse-rs?rev=e9e55a4529642da99e64452467ecaef9f7753531#e9e55a4529642da99e64452467ecaef9f7753531"
57 +source = "git+https://github.com/mozilla/cubeb-pulse-rs?rev=9695281319fcb3e40db6a32cc0661548d6192f4d#9695281319fcb3e40db6a32cc0661548d6192f4d"
61 diff --git a/third_party/rust/cubeb-pulse/.cargo-checksum.json b/third_party/rust/cubeb-pulse/.cargo-checksum.json
62 index 1dc452bcf480..45e913931336 100644
63 --- a/third_party/rust/cubeb-pulse/.cargo-checksum.json
64 +++ b/third_party/rust/cubeb-pulse/.cargo-checksum.json
66 -{"files":{".editorconfig":"bf047bd1da10cabb99eea666d1e57c321eba4716dccb3e4ed0e2c5fe3ca53858",".github/workflows/build.yml":"95d0d2542c04f0c932f58591b92c3051db5c95657bf5f24b6a6110f7b667568d","AUTHORS":"0e0ac930a68ce2f6b876126b195add177f0d3886facb9260f4d9b69f1988f0cc","Cargo.toml":"56e90cb82ec36ead07e551a28fc2455fa658fa8308c3d73f8d856d85bfcd2122","LICENSE":"44c6b5ae5ec3fe2fbc608b00e6f4896f4d2d5c7e525fcbaa3eaa3cf2f3d5a983","README.md":"0079450bb4b013bac065ed1750851e461a3710ebad1f323817da1cb82db0bc4f","src/backend/context.rs":"8969b300850b56ace07abe570940e2099774bf0bf20325be1d3ec0f5f408c8ce","src/backend/cork_state.rs":"4a0f1afc7d9f333dac89218cc56d7d32fbffb487cd48c1c9a4e03d79cb3b5e28","src/backend/intern.rs":"11ca424e4eb77f8eb9fd5a6717d1e791facf9743156a8534f0016fcf64d57b0f","src/backend/mod.rs":"d5da05348bf1a7f65c85b14372964a49dc4849f0aee96c75e2c18b51fb03fcaf","src/backend/stream.rs":"b17829f1b65055ada69454d0714dd7526d4d5cdeda7bba4ee03ffb6f27297e61","src/capi.rs":"fa0fa020f0d0efe55aa0fc3596405e8407bbe2cbe6c7a558345304e6da87994e","src/lib.rs":"b41bbdc562cbfb130ed7c1e53fe69944774f515705341d8ce48a2f82c8c0c2c5"},"package":null}
67 \ No newline at end of file
68 +{"files":{".editorconfig":"bf047bd1da10cabb99eea666d1e57c321eba4716dccb3e4ed0e2c5fe3ca53858",".github/workflows/build.yml":"95d0d2542c04f0c932f58591b92c3051db5c95657bf5f24b6a6110f7b667568d","AUTHORS":"0e0ac930a68ce2f6b876126b195add177f0d3886facb9260f4d9b69f1988f0cc","Cargo.toml":"56e90cb82ec36ead07e551a28fc2455fa658fa8308c3d73f8d856d85bfcd2122","LICENSE":"44c6b5ae5ec3fe2fbc608b00e6f4896f4d2d5c7e525fcbaa3eaa3cf2f3d5a983","README.md":"0079450bb4b013bac065ed1750851e461a3710ebad1f323817da1cb82db0bc4f","src/backend/context.rs":"8969b300850b56ace07abe570940e2099774bf0bf20325be1d3ec0f5f408c8ce","src/backend/cork_state.rs":"4a0f1afc7d9f333dac89218cc56d7d32fbffb487cd48c1c9a4e03d79cb3b5e28","src/backend/intern.rs":"11ca424e4eb77f8eb9fd5a6717d1e791facf9743156a8534f0016fcf64d57b0f","src/backend/mod.rs":"d5da05348bf1a7f65c85b14372964a49dc4849f0aee96c75e2c18b51fb03fcaf","src/backend/stream.rs":"585b616d5487c77f6aa21a76aa6a0f7ae9de8c389bc908401a979fba8bf3450f","src/capi.rs":"fa0fa020f0d0efe55aa0fc3596405e8407bbe2cbe6c7a558345304e6da87994e","src/lib.rs":"b41bbdc562cbfb130ed7c1e53fe69944774f515705341d8ce48a2f82c8c0c2c5"},"package":null}
69 \ No newline at end of file
70 diff --git a/third_party/rust/cubeb-pulse/src/backend/stream.rs b/third_party/rust/cubeb-pulse/src/backend/stream.rs
71 index 8168c813e243..df64b0f2304e 100644
72 --- a/third_party/rust/cubeb-pulse/src/backend/stream.rs
73 +++ b/third_party/rust/cubeb-pulse/src/backend/stream.rs
74 @@ -15,7 +15,7 @@ use ringbuf::RingBuffer;
75 use std::ffi::{CStr, CString};
76 use std::os::raw::{c_long, c_void};
78 -use std::sync::atomic::{AtomicUsize, Ordering};
79 +use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
82 use self::LinearInputBuffer::*;
83 @@ -272,7 +272,7 @@ pub struct PulseStream<'ctx> {
84 input_stream: Option<pulse::Stream>,
85 data_callback: ffi::cubeb_data_callback,
86 state_callback: ffi::cubeb_state_callback,
87 - drain_timer: *mut pa_time_event,
88 + drain_timer: AtomicPtr<pa_time_event>,
89 output_sample_spec: pulse::SampleSpec,
90 input_sample_spec: pulse::SampleSpec,
91 // output frames count excluding pre-buffering
92 @@ -411,7 +411,7 @@ impl<'ctx> PulseStream<'ctx> {
96 - drain_timer: ptr::null_mut(),
97 + drain_timer: AtomicPtr::new(ptr::null_mut()),
98 output_sample_spec: pulse::SampleSpec::default(),
99 input_sample_spec: pulse::SampleSpec::default(),
100 output_frame_count: AtomicUsize::new(0),
101 @@ -574,9 +574,10 @@ impl<'ctx> PulseStream<'ctx> {
102 self.context.mainloop.lock();
104 if let Some(stm) = self.output_stream.take() {
105 - if !self.drain_timer.is_null() {
106 + let drain_timer = self.drain_timer.load(Ordering::Acquire);
107 + if !drain_timer.is_null() {
108 /* there's no pa_rttime_free, so use this instead. */
109 - self.context.mainloop.get_api().time_free(self.drain_timer);
110 + self.context.mainloop.get_api().time_free(drain_timer);
112 stm.clear_state_callback();
113 stm.clear_write_callback();
114 @@ -637,7 +638,7 @@ impl<'ctx> StreamOps for PulseStream<'ctx> {
115 self.shutdown = true;
116 // If draining is taking place wait to finish
117 cubeb_log!("Stream stop: waiting for drain.");
118 - while !self.drain_timer.is_null() {
119 + while !self.drain_timer.load(Ordering::Acquire).is_null() {
120 self.context.mainloop.wait();
122 cubeb_log!("Stream stop: waited for drain.");
123 @@ -988,11 +989,12 @@ impl<'ctx> PulseStream<'ctx> {
125 cubeb_logv!("Drain finished callback.");
126 let stm = unsafe { &mut *(u as *mut PulseStream) };
127 - debug_assert_eq!(stm.drain_timer, e);
128 + let drain_timer = stm.drain_timer.load(Ordering::Acquire);
129 + debug_assert_eq!(drain_timer, e);
130 stm.state_change_callback(ffi::CUBEB_STATE_DRAINED);
131 /* there's no pa_rttime_free, so use this instead. */
132 - a.time_free(stm.drain_timer);
133 - stm.drain_timer = ptr::null_mut();
134 + a.time_free(drain_timer);
135 + stm.drain_timer.store(ptr::null_mut(), Ordering::Release);
136 stm.context.mainloop.signal();
139 @@ -1109,13 +1111,16 @@ impl<'ctx> PulseStream<'ctx> {
141 /* pa_stream_drain is useless, see PA bug# 866. this is a workaround. */
142 /* arbitrary safety margin: double the current latency. */
143 - debug_assert!(self.drain_timer.is_null());
144 + debug_assert!(self.drain_timer.load(Ordering::Acquire).is_null());
145 let stream_ptr = self as *const _ as *mut _;
146 if let Some(ref context) = self.context.context {
147 - self.drain_timer = context.rttime_new(
148 - pulse::rtclock_now() + 2 * latency,
151 + self.drain_timer.store(
152 + context.rttime_new(
153 + pulse::rtclock_now() + 2 * latency,
160 self.shutdown = true;
161 diff --git a/toolkit/library/rust/shared/Cargo.toml b/toolkit/library/rust/shared/Cargo.toml
162 index b70f8974ffb1..7d1d601c0807 100644
163 --- a/toolkit/library/rust/shared/Cargo.toml
164 +++ b/toolkit/library/rust/shared/Cargo.toml
165 @@ -20,7 +20,7 @@ profiler_helper = { path = "../../../../tools/profiler/rust-helper", optional =
166 mozurl = { path = "../../../../netwerk/base/mozurl" }
167 webrender_bindings = { path = "../../../../gfx/webrender_bindings" }
168 cubeb-coreaudio = { git = "https://github.com/mozilla/cubeb-coreaudio-rs", rev = "4ee3b283182926f9a8bb31d603219ff2db70d2fd", optional = true }
169 -cubeb-pulse = { git = "https://github.com/mozilla/cubeb-pulse-rs", rev="e9e55a4529642da99e64452467ecaef9f7753531", optional = true, features=["pulse-dlopen"] }
170 +cubeb-pulse = { git = "https://github.com/mozilla/cubeb-pulse-rs", rev="9695281319fcb3e40db6a32cc0661548d6192f4d", optional = true, features=["pulse-dlopen"] }
171 cubeb-sys = { version = "0.9", optional = true, features=["gecko-in-tree"] }
172 encoding_glue = { path = "../../../../intl/encoding_glue" }
173 audioipc-client = { git = "https://github.com/mozilla/audioipc-2", rev = "8381f3812354d8d1df972c2c3ea596b70caa9a84", optional = true }