Skip to content

Conversation

tisonkun
Copy link
Contributor

@tisonkun tisonkun commented Oct 3, 2025

Avoid global static IS_TEARING_DOWN trick.

Comment on lines +473 to +480
pub(crate) fn flush_sinks_atexit(&self) {
self.sinks.iter().for_each(|sink| {
if let Err(err) = sink.flush_atexit() {
self.handle_error(err);
}
});
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This may be even pub if users set their own global static LOGGER. But given that is not quite possible it's OK to leave pub(crate) until real use cases occur.

Copy link
Owner

@SpriteOvO SpriteOvO left a comment

Choose a reason for hiding this comment

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

I like this idea! In fact, previously we still couldn't guarantee that users wouldn't use thread-local (or anything else that might conflict with atexit()) in their own sinks, because the global atomic bool variable IS_TEARING_DOWN is not public. This PR enables users to be aware that the program is in an atexit() callback and customize the flush implementation for this case.

Perhaps one minor nitpick is that I'm not sure if there might be a better choice for the naming. flush_atexit implies that it will be called in the atexit() callback, which is good, on the other hand the naming style is not so Rusty... I'll think about it some more, anyway it's not a big issue.

// before we do that we have to destroy the thread pool to ensure that any
// pending log tasks are completed.
self.thread_pool.destroy();
self.backend.flush()
Copy link
Owner

Choose a reason for hiding this comment

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

We should also flush the underlying sub-sinks with flush_atexit method too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIUC self.backend.flush() already does it?

This is an identical refactor from the previous code snippet.

Copy link
Owner

Choose a reason for hiding this comment

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

The point here is that sub-sinks are agnostic to AsyncPoolSink. If a sub-sink is user-defined, it may still use thread-local in its flush(), and it won't know if the program is tearing down.

In other words, as a combined sink, I think AsyncPoolSink should forward flush_atexit to its sub-sinks, so that it can propagate the information that "this flush is the last flush, you are currently in the atexit callback".

Copy link
Owner

Choose a reason for hiding this comment

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

Currently, self.backend.flush() is still calling .flush() for sub-sinks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah. I got it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
self.backend.flush()
self.backend.flush_atexit()

Copy link
Owner

Choose a reason for hiding this comment

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

And please also apply a flush_atexit override for DedupSink, as it's also a conbined-sink.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved at 3e52f81. Although the name looks more terrible.

@tisonkun
Copy link
Contributor Author

tisonkun commented Oct 3, 2025

on the other hand the naming style is not so Rusty

Yeah. I'm struggling with naming this method also. Let's give it some days for potential new ideas :D

Comment on lines +473 to +479
pub(crate) fn flush_sinks_atexit(&self) {
self.sinks.iter().for_each(|sink| {
if let Err(err) = sink.flush_atexit() {
self.handle_error(err);
}
});
}
Copy link
Owner

@SpriteOvO SpriteOvO Oct 3, 2025

Choose a reason for hiding this comment

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

Nitpick, for better maintainability

fn flush_sinks_with<'a>(&'a self, with: impl Fn(&'a dyn Sink) -> Result<()>) {
    self.sinks.iter().for_each(|sink| {
        if let Err(err) = with(&**sink) {
            self.handle_error(err);
        }
    });
}

fn flush_sinks_atexit(&self) {
    self.flush_with(Sink::flush_atexit);
}

fn flush_sinks(&self) {
    self.flush_with(Sink::flush);
}

Same thing for AsyncPoolSink and DedupSink.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants