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

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

Merged

Conversation

ericsnowcurrently
Copy link
Member

@ericsnowcurrently ericsnowcurrently commented Jun 6, 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 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.

@ericsnowcurrently
Copy link
Member Author

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


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

Choose a reason for hiding this comment

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

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

Copy link
Member

Choose a reason for hiding this comment

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

Btw, why does it need asyncio? 🤔

Copy link
Member

Choose a reason for hiding this comment

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

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

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

@@ -6781,6 +6782,47 @@ def test_datetime_from_timestamp(self):
self.assertEqual(dt_orig, dt_rt)


class ExtensionModuleTests(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

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

@cpython_only?

Copy link
Member Author

Choose a reason for hiding this comment

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

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 ericsnowcurrently merged commit b2e71ff into python:main Jun 14, 2024
36 checks passed
@miss-islington-app
Copy link

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

@ericsnowcurrently 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
Copy link

bedevere-app bot commented Jun 14, 2024

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

@bedevere-app 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>
@ericsnowcurrently
Copy link
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)

@ericsnowcurrently
Copy link
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.
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