Skip to content

Commit

Permalink
appender: remove Sync bound from writer for NonBlocking (tokio-rs…
Browse files Browse the repository at this point in the history
…#2607)

## Motivation

`NonBlocking` from `tracing-appender` wraps a writer and requires that
writer to implement `Sync` (among other bounds). However it is not clear
why this bound is necessary in first place: this writer is sent to a
dedicated thread and never used directly concurrently.

tokio-rs#1934 demonstrates that it is a real problem for some people. Also at my
current work we hit this issue as well when a third-party writer did not
implement `Sync`. Our workaround was to wrap that writer in our own type
and manually implement `Send` and `Sync` for it. Needless to say that it
is more a hack than a proper solution.

## Solution

Remove `Sync` bound in relevant places. Yep, that simple. Probably
having it in first place was just an oversight.

Closes tokio-rs#1934
  • Loading branch information
AnthonyMikh authored and kaffarell committed Feb 14, 2024
1 parent 0ad76b2 commit d17f3e9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion tracing-appender/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub(crate) mod sync;
/// });
/// # }
/// ```
pub fn non_blocking<T: Write + Send + Sync + 'static>(writer: T) -> (NonBlocking, WorkerGuard) {
pub fn non_blocking<T: Write + Send + 'static>(writer: T) -> (NonBlocking, WorkerGuard) {
NonBlocking::new(writer)
}

Expand Down
6 changes: 3 additions & 3 deletions tracing-appender/src/non_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ impl NonBlocking {
///
/// [default]: NonBlockingBuilder::default
/// [builder]: NonBlockingBuilder
pub fn new<T: Write + Send + Sync + 'static>(writer: T) -> (NonBlocking, WorkerGuard) {
pub fn new<T: Write + Send + 'static>(writer: T) -> (NonBlocking, WorkerGuard) {
NonBlockingBuilder::default().finish(writer)
}

fn create<T: Write + Send + Sync + 'static>(
fn create<T: Write + Send + 'static>(
writer: T,
buffered_lines_limit: usize,
is_lossy: bool,
Expand Down Expand Up @@ -221,7 +221,7 @@ impl NonBlockingBuilder {
}

/// Completes the builder, returning the configured `NonBlocking`.
pub fn finish<T: Write + Send + Sync + 'static>(self, writer: T) -> (NonBlocking, WorkerGuard) {
pub fn finish<T: Write + Send + 'static>(self, writer: T) -> (NonBlocking, WorkerGuard) {
NonBlocking::create(
writer,
self.buffered_lines_limit,
Expand Down
4 changes: 2 additions & 2 deletions tracing-appender/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::Debug;
use std::io::Write;
use std::{io, thread};

pub(crate) struct Worker<T: Write + Send + Sync + 'static> {
pub(crate) struct Worker<T: Write + Send + 'static> {
writer: T,
receiver: Receiver<Msg>,
shutdown: Receiver<()>,
Expand All @@ -18,7 +18,7 @@ pub(crate) enum WorkerState {
Shutdown,
}

impl<T: Write + Send + Sync + 'static> Worker<T> {
impl<T: Write + Send + 'static> Worker<T> {
pub(crate) fn new(receiver: Receiver<Msg>, writer: T, shutdown: Receiver<()>) -> Worker<T> {
Self {
writer,
Expand Down

0 comments on commit d17f3e9

Please sign in to comment.