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

mdc-utils: Make LoggerStringWriter thread friendly #2771

Conversation

bryce-anderson
Copy link
Contributor

@bryce-anderson bryce-anderson commented Dec 6, 2023

Motivation:

We have seen some flakyness in HttpMessageDiscardWatchdogServiceFilterTest that may be related to thread safety for the test log appender. Specifically,

  • The sink, a StringWriter, is globally shared so it can be reset by concurrently running tests.
  • The underlying StringWriter itself isn't thread safe and it's possible for it to be concurrent written to and read from.

Modifications:

  • Make a new (thread safe) StringWriter for each thread. Based on the usage pattern for the LoggerStringWriter methods this should have the effect of having a unique writer per test.
  • Make a thread-safe proxy for the StringWriter so it's safe to write and read from concurrently.

Closes #2756. (hopefully)

Motivation:

We have seen some flakyness in HttpMessageDiscardWatchdogServiceFilterTest
that may be related to thread safety for the test log appender.
Specifically,
- The sink, a StringWriter, is globally shared so it can be reset by
  concurrently running tests.
- The underlying StringWriter itself isn't thread safe and it's possible
  for it to be concurrent written to and read from.

Modifications:

- Make a new (thread safe) StringWriter for each thread. Based on the
  usage pattern for the LoggerStringWriter methods this should have the
  effect of having a unique writer per test.
- Make a thread-safe proxy for the StringWriter so it's safe to write
  and read from concurrently.
Copy link
Contributor

@daschl daschl left a comment

Choose a reason for hiding this comment

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

Looks good to me, consider adding a note on the APIs that from now on you are only allowed to consume the results from the same thread you started with the reset.

@bryce-anderson bryce-anderson force-pushed the bl_anderson/flaky_HttpMessageDiscardWatchdogServiceFilterTest branch from a32b203 to a93e1e3 Compare December 6, 2023 17:16
@bryce-anderson
Copy link
Contributor Author

Looks good to me, consider adding a note on the APIs that from now on you are only allowed to consume the results from the same thread you started with the reset.

Your comment was spot on, and made it clear to me that the thread-local stuff is just a hack to preserve the static API that isn't worth it. So, I made LoggerStringWriter something you use as an instance.

@bryce-anderson bryce-anderson force-pushed the bl_anderson/flaky_HttpMessageDiscardWatchdogServiceFilterTest branch from a93e1e3 to 0b7c5c2 Compare December 6, 2023 17:53
@bryce-anderson bryce-anderson merged commit 112fb07 into apple:main Dec 7, 2023
15 checks passed
@bryce-anderson bryce-anderson deleted the bl_anderson/flaky_HttpMessageDiscardWatchdogServiceFilterTest branch December 7, 2023 16:08
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.

HttpMessageDiscardWatchdogServiceFilterTest.warnsIfDiscarded test failure
3 participants