FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-120161: Fix a Crash in the _datetime Module by ericsnowcurrently · Pull Request #120182 · python/cpython · GitHub

/ cpython Public

gh-120161: Fix a Crash in the _datetime Module - #120182

Merged
ericsnowcurrently merged 7 commits into
python:mainfrom
ericsnowcurrently:fix-datetime-creash-gh-120161
Jun 14, 2024
Merged

ericsnowcurrently merged 7 commits into
python:mainfrom
ericsnowcurrently:fix-datetime-creash-gh-120161

Conversation

ericsnowcurrently commented Jun 6, 2024 •
edited
Loading

Copy link
Copy Markdown
Member

In gh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown. However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection. The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection. To avoid the problem for _datetime, I applied a similar approach here.

Also, credit goes to @mgorny and @neonene for the new tests.

FYI, I'd like to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting. I'll circle back to the cleaner approach on main after this is merged.

Copy link
Copy Markdown
Member Author

FYI, I'm planning on merging this Friday, barring any feedback.


with self.subTest('complex'):
script = textwrap.dedent("""
import asyncio

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I would separate this test into its own method, it imports asyncio. Which means that it needs @requires_working_socket() decorator

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Btw, why does it need asyncio? 🤔

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Because of the original issue (the snippet is taken almost verbatim).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Importing asyncio on its own is not enough to be a problem. I was able to verify that it works fine on a WASI build (where @requires_working_socket() would skip). This is also verified by the WASI CI check passing. Unless there is another reason to do so, I'd rather not add that decorator.

self.assertEqual(dt_orig, dt_rt)


class ExtensionModuleTests(unittest.TestCase):

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@cpython_only?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We could, but currently there's mostly no need. If the extension module ("_datetime") doesn't exist then the "fast" version of the test is never created, and the pure-Python version of the test already always skips. (See Lib/test/test_datetime.py.) Also note that @cpython_only would mean the test wouldn't be run for Python implementations that do provide a _datetime accelerator module.

All that said, I don't see any harm with adding @cpython_only on the new test method. I would certainly only expect it to fail on CPython. Thanks for bringing this up.

ericsnowcurrently merged commit b2e71ff into python:main Jun 14, 2024

Copy link
Copy Markdown

Thanks @ericsnowcurrently for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

ericsnowcurrently deleted the fix-datetime-creash-gh-120161 branch June 14, 2024 19:29
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 14, 2024
In pythongh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown.  However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection.  The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection.  To avoid the problem for _datetime, I have applied a similar approach here.

Also, credit goes to @mgorny and @neonene for the new tests.

FYI, I would have liked to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting.  I'll circle back to the cleaner approach with a future change on the main branch.
(cherry picked from commit b2e71ff)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>

bedevere-app Bot commented Jun 14, 2024

Copy link
Copy Markdown

GH-120518 is a backport of this pull request to the 3.13 branch.

bedevere-app Bot removed the needs backport to 3.13 bugs and security fixes label Jun 14, 2024
ericsnowcurrently added a commit that referenced this pull request Jun 14, 2024
In gh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown.  However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection.  The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection.  To avoid the problem for _datetime, I have applied a similar approach here.

Also, credit goes to @mgorny and @neonene for the new tests.

FYI, I would have liked to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting.  I'll circle back to the cleaner approach with a future change on the main branch.

(cherry picked from commit b2e71ff, AKA gh-120182)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>

Copy link
Copy Markdown
Member Author

Looks like this broke some buildbots:

test_create_many_threaded (test.test_interpreters.test_stress.StressTests.test_create_many_threaded) ... python: Objects/typeobject.c:248: managed_static_type_state_init: Assertion `(initial == 1) == (_PyRuntime.types.managed_static.types[full_index].interp_count == 0)' failed.
Aborted (core dumped)

Copy link
Copy Markdown
Member Author

See gh-120524.

ericsnowcurrently added a commit that referenced this pull request Jun 17, 2024
…nterp_count (gh-120529)

gh-120182 added new global state (interp_count), but didn't add thread-safety for it.  This change eliminates the possible race.
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 17, 2024
…s[i].interp_count (pythongh-120529)

pythongh-120182 added new global state (interp_count), but didn't add thread-safety for it.  This change eliminates the possible race.
(cherry picked from commit 2c66318)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
ericsnowcurrently added a commit that referenced this pull request Jun 17, 2024
…es[i].interp_count (gh-120657)

gh-120182 added new global state (interp_count), but didn't add thread-safety for it.  This change eliminates the possible race.

(cherry picked from commit 2c66318, AKA gh-120529)

Co-authored-by: Eric Snow <ericsnowcurrently@gmail.com>
mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
In pythongh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown.  However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection.  The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection.  To avoid the problem for _datetime, I have applied a similar approach here.

Also, credit goes to @mgorny and @neonene for the new tests.

FYI, I would have liked to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting.  I'll circle back to the cleaner approach with a future change on the main branch.
mrahtz pushed a commit to mrahtz/cpython that referenced this pull request Jun 30, 2024
…s[i].interp_count (pythongh-120529)

pythongh-120182 added new global state (interp_count), but didn't add thread-safety for it.  This change eliminates the possible race.
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
In pythongh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown.  However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection.  The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection.  To avoid the problem for _datetime, I have applied a similar approach here.

Also, credit goes to @mgorny and @neonene for the new tests.

FYI, I would have liked to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting.  I'll circle back to the cleaner approach with a future change on the main branch.
noahbkim pushed a commit to hudson-trading/cpython that referenced this pull request Jul 11, 2024
…s[i].interp_count (pythongh-120529)

pythongh-120182 added new global state (interp_count), but didn't add thread-safety for it.  This change eliminates the possible race.
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
In pythongh-120009 I used an atexit hook to finalize the _datetime module's static types at interpreter shutdown.  However, atexit hooks are executed very early in finalization, which is a problem in the few cases where a subclass of one of those static types is still alive until the final GC collection.  The static builtin types don't have this probably because they are finalized toward the end, after the final GC collection.  To avoid the problem for _datetime, I have applied a similar approach here.

Also, credit goes to @mgorny and @neonene for the new tests.

FYI, I would have liked to take a slightly cleaner approach with managed static types, but wanted to get a smaller fix in first for the sake of backporting.  I'll circle back to the cleaner approach with a future change on the main branch.
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
…s[i].interp_count (pythongh-120529)

pythongh-120182 added new global state (interp_count), but didn't add thread-safety for it.  This change eliminates the possible race.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL