Skip to content

Add recursion depth limit to prevent stack overflow in hash function#342

Merged
shaypal5 merged 2 commits intocodex/add-test-and-benchmark-for-numpy-array-performancefrom
copilot/sub-pr-337
Feb 18, 2026
Merged

Add recursion depth limit to prevent stack overflow in hash function#342
shaypal5 merged 2 commits intocodex/add-test-and-benchmark-for-numpy-array-performancefrom
copilot/sub-pr-337

Conversation

Copy link
Contributor

Copilot AI commented Feb 18, 2026

The recursive _update_hash_for_value function could cause stack overflow when hashing deeply nested data structures (lists, dicts, tuples).

Changes

  • Added depth tracking: _update_hash_for_value now tracks recursion depth and enforces a configurable limit (default: 100 levels)
  • Clear error handling: Raises RecursionError with actionable guidance when limit is exceeded
  • Comprehensive tests: Added tests/test_recursion_depth.py covering edge cases for lists, dicts, and tuples at various nesting levels

Behavior

Normal nested structures continue to work:

# 50 levels deep - works fine
nested = []
current = nested
for _ in range(50):
    inner = []
    current.append(inner)
    current = inner

@cachier()
def process(data):
    return "ok"

process(nested)  # ✓ Cached successfully

Pathological cases now fail fast with clear guidance:

# 150 levels deep - raises RecursionError
nested = []
current = nested
for _ in range(150):
    inner = []
    current.append(inner)
    current = inner

process(nested)
# RecursionError: Maximum recursion depth (100) exceeded while hashing nested 
# data structure. Consider flattening your data or using a custom hash_func parameter.

The implementation is backward compatible - new parameters use defaults that preserve existing behavior for typical use cases.


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…h check

Co-authored-by: shaypal5 <917954+shaypal5@users.noreply.github.com>
Copilot AI changed the title [WIP] WIP Address feedback from NumPy-aware hashing review Add recursion depth limit to prevent stack overflow in hash function Feb 18, 2026
Copilot AI requested a review from shaypal5 February 18, 2026 17:23
@shaypal5 shaypal5 marked this pull request as ready for review February 18, 2026 17:30
@shaypal5 shaypal5 merged commit b2ba0ba into codex/add-test-and-benchmark-for-numpy-array-performance Feb 18, 2026
1 check failed
@shaypal5 shaypal5 deleted the copilot/sub-pr-337 branch February 18, 2026 17:30
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.

2 participants