python / python/cpython

OrderedDict.pop() can segfault when key equality changes between lookups

Open
#154,817 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

interpreter-core type-crash
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Crash report

What happened?

Calling collections.OrderedDict.pop() without a default value can cause a segmentation fault if the lookup key has a stateful __eq__ implementation that returns different results across successive comparisons.

The crash occurs on the current main branch.

Minimal reproducer:

from collections import OrderedDict

class K:
    def __init__(self):
        self.calls = 0

    def __hash__(self):
        return 12345

    def __eq__(self, other):
        self.calls += 1
        return self.calls == 1

k1 = K()
k2 = K()

od = OrderedDict()
od[k1] = "value"

od.pop(k2)

Running this with a locally built CPython from main results in:

Segmentation fault (core dumped)

Expected behavior:

  • OrderedDict.pop() should not crash the interpreter.
  • It should either remove the matching item or raise KeyError if the key is not found.
CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.16.0a0 (heads/fix-odict-pop-null-failobj:2ffab083782, Jul 28 2026, 12:31:11) [GCC 13.3.0]

Linked PRs
  • gh-154818

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the OrderedDict.pop() entry point and run the minimal reproducer on a locally built CPython main branch. Done means the reproducer no longer segfaults and pop() either removes the matching item or raises KeyError when no key is found.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.