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

subscriber: fix missing on_layer for Box/Arc layer impls #1576

Merged
merged 5 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
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
9 changes: 9 additions & 0 deletions tracing-subscriber/src/filter/layer_filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ impl<L, F, S> Filtered<L, F, S> {

#[inline(always)]
fn id(&self) -> FilterId {
debug_assert!(
!self.id.0.is_disabled(),
"a `Filtered` layer was used, but it had no `FilterId`; \
was it registered with the subscriber?"
);
self.id.0
}

Expand Down Expand Up @@ -504,6 +509,10 @@ impl FilterId {

Self(self.0 | other)
}

fn is_disabled(self) -> bool {
self.0 == Self::disabled().0
}
}

impl fmt::Debug for FilterId {
Expand Down
32 changes: 31 additions & 1 deletion tracing-subscriber/src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,11 @@
//! [`LevelFilter`]: crate::filter::LevelFilter
//! [feat]: crate#feature-flags
use crate::filter;
use std::{any::TypeId, ops::Deref, sync::Arc};
use std::{
any::TypeId,
ops::{Deref, DerefMut},
sync::Arc,
};
use tracing_core::{
metadata::Metadata,
span,
Expand Down Expand Up @@ -1160,13 +1164,31 @@ where
L: Layer<S>,
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
if let Some(inner) = Arc::get_mut(self) {
// XXX(eliza): this may behave weird if another `Arc` clone of this
// layer is layered onto a _different_ subscriber...but there's no
// good solution for that...
Comment on lines +1169 to +1171
Copy link
Contributor

@jsgf jsgf Sep 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment. Arc::get_mut will only return a value if there are no other aliases so if it gets here there can't be a different subscriber. So the weird behaviour will be if there are multiple references to the Layer then this won't get called.

It seems like Arc<Layer> and on_layer are intrinsically incompatible. If having on_layer(&mut self, ... is useful, then maybe there shouldn't be Arc after all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arc::get_mut will only return a value if there are no other aliases so if it gets here there can't be a different subscriber.

was thinking about a situation where the Arc is cloned after on_layer is called. But, in practice, that will never happen, because on_layer is only called when layering, and layering moves the layer by value. However, there's nothing stopping user code from just arbitrarily calling on_layer.

It seems like Arc<Layer> and on_layer are intrinsically incompatible. If having on_layer(&mut self, ... is useful, then maybe there shouldn't be Arc after all?

Yes, I'm thinking that as well --- I regret not thinking through the potential implications of the Arc impl w.r.t having an on_layer callback. We may want to deprecate the Arc impls...which doesn't feel great, since we just added them...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...ugh, i've been once again reminded that you can't deprecate a trait impl for a type:
image

so, I guess the only real solution for this is a big warning in the docs and removing it in the next breaking change. Gah!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to deprecate a bad idea earlier than later. And if Arc isn't actually usable, then presumably that will dissuade people from using it.

inner.on_layer(subscriber);
}
}

layer_impl_body! {}
}

impl<S> Layer<S> for Arc<dyn Layer<S> + Send + Sync>
where
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
if let Some(inner) = Arc::get_mut(self) {
// XXX(eliza): this may behave weird if another `Arc` clone of this
// layer is layered onto a _different_ subscriber...but there's no
// good solution for that...
inner.on_layer(subscriber);
}
}

layer_impl_body! {}
}

Expand All @@ -1175,13 +1197,21 @@ where
L: Layer<S>,
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
self.deref_mut().on_layer(subscriber);
}

layer_impl_body! {}
}

impl<S> Layer<S> for Box<dyn Layer<S> + Send + Sync>
where
S: Subscriber,
{
fn on_layer(&mut self, subscriber: &mut S) {
self.deref_mut().on_layer(subscriber);
}

layer_impl_body! {}
}

Expand Down
73 changes: 73 additions & 0 deletions tracing-subscriber/tests/layer_filters/boxed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use super::*;
use std::sync::Arc;
use tracing_subscriber::{filter, prelude::*, Layer};

fn layer() -> (ExpectLayer, subscriber::MockHandle) {
layer::mock().done().run_with_handle()
}

fn filter<S>() -> filter::DynFilterFn<S> {
// Use dynamic filter fn to disable interest caching and max-level hints,
// allowing us to put all of these tests in the same file.
filter::dynamic_filter_fn(|_, _| false)
}

/// reproduces https://github.com/tokio-rs/tracing/issues/1563#issuecomment-921363629
#[test]
fn box_works() {
let (layer, handle) = layer();
let layer = Box::new(layer.with_filter(filter()));

let _guard = tracing_subscriber::registry().with(layer).set_default();

for i in 0..2 {
tracing::info!(i);
}

handle.assert_finished();
}

/// the same as `box_works` but with a type-erased `Box`.
#[test]
fn dyn_box_works() {
let (layer, handle) = layer();
let layer: Box<dyn Layer<_> + Send + Sync + 'static> = Box::new(layer.with_filter(filter()));

let _guard = tracing_subscriber::registry().with(layer).set_default();

for i in 0..2 {
tracing::info!(i);
}

handle.assert_finished();
}

/// the same as `box_works` but with an `Arc`.
#[test]
fn arc_works() {
let (layer, handle) = layer();
let layer = Box::new(layer.with_filter(filter()));

let _guard = tracing_subscriber::registry().with(layer).set_default();

for i in 0..2 {
tracing::info!(i);
}

handle.assert_finished();
}

/// the same as `box_works` but with a type-erased `Arc`.
#[test]
fn dyn_arc_works() {
let (layer, handle) = layer();
let layer: Arc<dyn Layer<_> + Send + Sync + 'static> = Arc::new(layer.with_filter(filter()));

let _guard = tracing_subscriber::registry().with(layer).set_default();

for i in 0..2 {
tracing::info!(i);
}

handle.assert_finished();
}
1 change: 1 addition & 0 deletions tracing-subscriber/tests/layer_filters/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#[path = "../support.rs"]
mod support;
use self::support::*;
mod boxed;
mod filter_scopes;
mod targets;
mod trees;
Expand Down