Skip to content

GH-113421: Fix multiprocessing logger #113423

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

Merged

Conversation

xu-song
Copy link
Contributor

@xu-song xu-song commented Dec 23, 2023

@ghost
Copy link

ghost commented Dec 23, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Dec 23, 2023

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@xu-song xu-song changed the title GH-113422: Fix multiprocessing logger GH-113421: Fix multiprocessing logger Dec 23, 2023
@gaogaotiantian
Copy link
Member

The fix looks reasonable to me. You should probably add a regression test and a news entry to it.

@xu-song xu-song force-pushed the gh-113421-fix_multiprocessing_logger branch from 59d7229 to 772a742 Compare December 23, 2023 08:11
@bedevere-app
Copy link

bedevere-app bot commented Dec 23, 2023

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@xu-song
Copy link
Contributor Author

xu-song commented Dec 23, 2023

Test case and news entry have been added to this pull request.

@serhiy-storchaka serhiy-storchaka self-requested a review December 23, 2023 10:15
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

The test does not fail with unmodified code. But it produces superfluous output and warnings (because it modifies the global handlers list).

Please make a test that doues not produce unneeded output and warnings, fails when the bug is not fixed, and success if it is fixed.

@bedevere-app
Copy link

bedevere-app bot commented Dec 23, 2023

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@xu-song
Copy link
Contributor Author

xu-song commented Dec 23, 2023

@serhiy-storchaka
I have made the requested changes; please review again.

@gaogaotiantian
Copy link
Member

@xu-song

You test still does NOT fail.

A regression test should:

  • Fail for unmodified code (your test does NOT). You should add some self.assert* to make this happen. Check the docs for unittest.
  • Pass after your modification. In this case, you should check the output of the logger for some specific strings (filenames).
  • Keep the original state before the test executes as much as possible. You added a handler to multiprocess logger and never remove it. You restored the log level, but the better way to do it is to restore the value to what it was, not a hardcoded LOG_LEVEL. You should also consider the cases when the test failed in the middle of your code and how to restore the state in those cases (try ... finally ... block).

In practice, the following should happen:

WITHOUT your source code changes (but with the regression test you added), ./python -m test test._test_multiprocessing should FAIL and report some error.

WITH your source code changes and the regression test you added, ./python -m test test._test_multiprocessing should PASS.

@xu-song xu-song marked this pull request as draft December 24, 2023 03:28
Copy link
Member

@gaogaotiantian gaogaotiantian left a comment

Choose a reason for hiding this comment

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

I think you are doing a great job. That's what we meant for a regression test. I do have some suggestions to you, but that's basically what I got for now. I'll leave the final approval for @serhiy-storchaka .


def test_filename(self):
logger = multiprocessing.get_logger()
default_level = logger.level
Copy link
Member

Choose a reason for hiding this comment

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

I think original_level might be a more proper name for this. It does not have to be the default level.

util.info('2')
logger.debug('3')
filename = os.path.basename(__file__)
self.assert_log_lines([
Copy link
Member

Choose a reason for hiding this comment

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

I think having a utility function might be a bit of an overkill here. Your check is very thorough but it might be too strict. I think something like

output = stream.getvalue()
self.assertIn(f'[INFO] [{filename}] 1', output)
... 

should be good enough here. It's clear enough for people about what it's doing, and it's testing against the core issue this PR solves. Making sure the output matches exactly what's expected is not necessarily the best way to do it (especially if it makes the code harder to understand).

@xu-song
Copy link
Contributor Author

xu-song commented Dec 24, 2023

Thanks for your instruction. I have made the requested changes.

@xu-song xu-song marked this pull request as ready for review December 24, 2023 07:53
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@serhiy-storchaka serhiy-storchaka added needs backport to 3.11 only security fixes needs backport to 3.12 only security fixes labels Dec 24, 2023
@serhiy-storchaka serhiy-storchaka merged commit ce77ee5 into python:main Dec 24, 2023
@miss-islington-app
Copy link

Thanks @xu-song for the PR, and @serhiy-storchaka for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11, 3.12.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 24, 2023
…nGH-113423)

(cherry picked from commit ce77ee5)

Co-authored-by: Xu Song <xusong.vip@gmail.com>
@bedevere-app
Copy link

bedevere-app bot commented Dec 24, 2023

GH-113450 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 only security fixes label Dec 24, 2023
@bedevere-app
Copy link

bedevere-app bot commented Dec 24, 2023

GH-113451 is a backport of this pull request to the 3.11 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 24, 2023
…nGH-113423)

(cherry picked from commit ce77ee5)

Co-authored-by: Xu Song <xusong.vip@gmail.com>
@bedevere-app bedevere-app bot removed the needs backport to 3.11 only security fixes label Dec 24, 2023
@serhiy-storchaka
Copy link
Member

Thank you @xu-song for your contribution and @gaogaotiantian for guiding it.

serhiy-storchaka pushed a commit that referenced this pull request Dec 24, 2023
…13423) (GH-113451)

(cherry picked from commit ce77ee5)

Co-authored-by: Xu Song <xusong.vip@gmail.com>
serhiy-storchaka pushed a commit that referenced this pull request Dec 24, 2023
…13423) (GH-113450)

(cherry picked from commit ce77ee5)

Co-authored-by: Xu Song <xusong.vip@gmail.com>
ryan-duve pushed a commit to ryan-duve/cpython that referenced this pull request Dec 26, 2023
Glyphack pushed a commit to Glyphack/cpython that referenced this pull request Sep 2, 2024
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