Skip to content

Commit

Permalink
Use append() for writing telemetry lock to file (#585)
Browse files Browse the repository at this point in the history
Otherwise we see spurious race conditions where the file is opened twice and one thread is overwriting to the start of the file.

And lock cbindgen version for CI.
  • Loading branch information
bwoebi committed Aug 12, 2024
1 parent 651d5b4 commit b9d4da7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 8 additions & 4 deletions ddtelemetry/src/worker/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use ddcommon::HttpRequestBuilder;
use http::{Request, Response};
use hyper::Body;
use std::{
fs::File,
fs::OpenOptions,
future::Future,
io::Write,
pin::Pin,
Expand Down Expand Up @@ -46,7 +46,11 @@ pub fn from_config(c: &Config) -> Box<dyn HttpClient + Sync + Send> {
.expect("file urls should always have been encoded in authority");
return Box::new(MockClient {
file: Arc::new(Mutex::new(Box::new(
File::create(file_path).expect("Couldn't open mock client file"),
OpenOptions::new()
.create(true)
.append(true)
.open(file_path.as_path())
.expect("Couldn't open mock client file"),
))),
});
}
Expand Down Expand Up @@ -78,12 +82,12 @@ impl HttpClient for MockClient {
fn request(&self, mut req: Request<hyper::Body>) -> ResponseFuture {
let s = self.clone();
Box::pin(async move {
let body = hyper::body::to_bytes(req.body_mut()).await?;
let mut body = hyper::body::to_bytes(req.body_mut()).await?.to_vec();
body.push(b'\n');

{
let mut writer = s.file.lock().expect("mutex poisoned");
writer.write_all(body.as_ref()).unwrap();
writer.write_all(b"\n").unwrap();
}

Ok(Response::builder()
Expand Down
2 changes: 1 addition & 1 deletion tools/docker/Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ ENV PATH="/root/.cargo/bin:$PATH"
ARG CARGO_BUILD_INCREMENTAL
ARG CARGO_NET_RETRY
ENV CARGO_NET_RETRY="${CARGO_NET_RETRY}"
RUN cargo install cbindgen && cargo install bindgen-cli --locked && rm -rf /root/.cargo/registry /root/.cargo/git
RUN cargo install cbindgen --version "^0.26" && cargo install bindgen-cli --locked && rm -rf /root/.cargo/registry /root/.cargo/git

FROM alpine_aws_cli as alpine_builder
COPY --from=alpine_cbindgen /root/.cargo/bin/cbindgen /usr/local/bin/cbindgen
Expand Down

0 comments on commit b9d4da7

Please sign in to comment.