Skip to content

Build the struct before tp_alloc in PyStruct_copy - #807

Merged
ptomecek merged 1 commit into
Point72:mainfrom
emilybarrettp72:collector_collision
Dec 2, 2026
Merged

ptomecek merged 1 commit into
Point72:mainfrom
emilybarrettp72:collector_collision

Conversation

@emilybarrettp72

Copy link
Copy Markdown
Contributor
  • PyStruct_copy calls tp_alloc (GC-tracked, struct_ still null) before struct_->copy()
  • copy() → createRaw() deepcopies default values via python, which can trigger a GC collect → PyStruct_traverse derefs the null struct_ → segfault at 0xffffffffffffffe0
  • PyStruct_deepcopy already has this exact fix + comment; copy never got it
  • reorder to build the struct first
  • regression test: struct with a list default, gc.set_threshold(1, 1, 1), 10k copies; segfaults 3/3 on 0.19.1

copy() deepcopies default values, which can trigger a GC collect. tp_alloc had
already added the PyStruct to GC with a null struct_, so PyStruct_traverse
dereferenced it and segfaulted. Same fix PyStruct_deepcopy already has.

Signed-off-by: Emily Barrett <emily.barrett@cubistsystematic.com>
@ptomecek
ptomecek merged commit ede9bfb into Point72:main Dec 2, 2026
64 of 65 checks passed
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