Skip to content

Conversation

@marcdumais-work
Copy link
Contributor

@marcdumais-work marcdumais-work commented Jun 4, 2025

What it does

This field is accessed by various threads, whenever logging is happening. All accesses are synchronized except the empty check in timer thread, done before calling flush().

Once in a while, the record buffer is flushed to the writer queue and a new, empty buffer is created and assigned to "fRecordBuffer". If that happens just before the timer task does its empty check, that thread might still see the old buffer, and then the call to flush() might result in adding an empty buffer to the queue. To avoid that, the empty check has been moved inside the synchronized flush() function.

Also, for explicitness's sake, mark field fFileHandler as final. It's already used as such, and is only set in the constructor, but this makes it explicit.

How to test

Confirm that CI still passes.

Follow-ups

N/A

Review checklist

  • As an author, I have thoroughly tested my changes and carefully followed the instructions in this template

private volatile boolean fIsEnabled = true;

private List<LogRecord> fRecordBuffer = new ArrayList<>(fMaxSize);
private volatile List<LogRecord> fRecordBuffer = new ArrayList<>(fMaxSize);
Copy link
Contributor

Choose a reason for hiding this comment

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

The record buffer is already used with synchronization, except the empty check in the timer thread. This check could be moved to the flush() method and then all read and write accesses would always be synchronized.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree - I will update the PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update done

@marcdumais-work marcdumais-work force-pushed the asynchandler-volatile-fields branch from 695cf06 to b646dae Compare June 4, 2025 16:25
@marcdumais-work marcdumais-work changed the title [AsyncFileHandler] use 'volatile' for fRecordBuffer [AsyncFileHandler] insure good synchronization for 'fRecordBuffer' Jun 4, 2025
This field is accessed by various threads, whenever logging is
happening. All accesses are synchronized except the empty check
in timer thread, done before calling flush().

Once in a while, the record buffer is flushed to the writer queue
and a new, empty buffer is created and assigned to "fRecordBuffer".
If that happens just before the timer task does its empty check,
that thread might still see the old buffer, and then the call to
flush() might result in adding an empty buffer to the queue. To
avoid that, the empty check has been moved inside the synchronized
flush() function.

Also, for explicitness sake, mark field fFileHandler as final.
It's already used as such, and is only set in the constructor,
but this makes it explicit.

Signed-off-by: Marc Dumais <[email protected]>
@marcdumais-work marcdumais-work force-pushed the asynchandler-volatile-fields branch from b646dae to f280622 Compare June 4, 2025 16:27
@bhufmann bhufmann merged commit 77230ec into eclipse-tracecompass:main Jun 5, 2025
4 checks passed
@marcdumais-work marcdumais-work deleted the asynchandler-volatile-fields branch June 5, 2025 13:03
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.

3 participants