Skip to content

[3.0.x] BUG: fix segfault adding non-tick DateOffset to datetime data under threads#66031

Merged
jorisvandenbossche merged 1 commit into
pandas-dev:3.0.xfrom
jbrockmendel:offsets-nogil-day_opt-3.0.x
Jun 26, 2026
Merged

[3.0.x] BUG: fix segfault adding non-tick DateOffset to datetime data under threads#66031
jorisvandenbossche merged 1 commit into
pandas-dev:3.0.xfrom
jbrockmendel:offsets-nogil-day_opt-3.0.x

Conversation

@jbrockmendel

Copy link
Copy Markdown
Member

Backports the memory-safety core of GH-64480 to the 3.0.x branch.

The vectorized offset apply path (shift_months / shift_quarters_roll_qtrday / get_day_of_month) compared the Python str day_opt (day_opt == "start", etc.) inside with nogil: blocks — i.e. with the GIL released. When another thread runs Python concurrently (for example pytest-xdist's execnet receiver thread), that races the comparison on Python internals and can segfault. This is what crashes the cp311/cp312/cp313 3.0.x wheel builds in _add_offset during the test phase.

The fix converts day_opt to a C enum before the with nogil: blocks, so the in-nogil comparisons are pure C and touch no Python objects. It is the minimal subset of GH-64480 (just the day_opt enum) without the rest of that PR's perf changes; the public shift_months / shift_quarters / roll_qtrday signatures are unchanged.

main already has this via GH-64480, so no forward-port is needed. The bug has been latent since 2022, is platform/timing-dependent (manifests on Linux x86_64, not macOS arm64), and is numpy-independent.

…hreads

The vectorized offset apply path (shift_months/shift_quarters ->
_roll_qtrday/get_day_of_month) compared the Python ``str`` day_opt inside
``with nogil`` blocks, i.e. with the GIL released. Concurrent Python
activity in another thread (e.g. pytest-xdist's execnet receiver) could
race those comparisons and segfault.

Convert day_opt to a C enum before releasing the GIL so the in-nogil
comparisons are pure C. This backports the memory-safety core of the
day_opt-enum refactor in pandas-devGH-64480 without the rest of that PR's changes.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jbrockmendel jbrockmendel force-pushed the offsets-nogil-day_opt-3.0.x branch from 1c88351 to e3542d2 Compare June 26, 2026 00:16
@jorisvandenbossche jorisvandenbossche added the Build Library building on various platforms label Jun 26, 2026
@jorisvandenbossche jorisvandenbossche changed the title BUG: fix segfault adding non-tick DateOffset to datetime data under threads [3.0.x] BUG: fix segfault adding non-tick DateOffset to datetime data under threads Jun 26, 2026
@jorisvandenbossche

Copy link
Copy Markdown
Member

@jbrockmendel thanks! That seems to fix it indeed (still a bit strange that it only shows up on the wheel builds ..)

@jorisvandenbossche jorisvandenbossche merged commit 15494e8 into pandas-dev:3.0.x Jun 26, 2026
118 of 119 checks passed
@jbrockmendel jbrockmendel deleted the offsets-nogil-day_opt-3.0.x branch June 26, 2026 15:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Build Library building on various platforms

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants