Skip to content

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

Merged
shaypal5 merged 2 commits into
codex/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 into
codex/add-test-and-benchmark-for-numpy-array-performancefrom
copilot/sub-pr-337

Conversation

Copilot AI commented Feb 18, 2026

Copy link
Copy Markdown
Contributor

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
shaypal5 added a commit that referenced this pull request Mar 20, 2026
…342)

* Initial plan

* Add stack overflow protection to _update_hash_for_value with max depth check

Co-authored-by: shaypal5 <917954+shaypal5@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: shaypal5 <917954+shaypal5@users.noreply.github.com>
@shaypal5 shaypal5 added AI & Agents area: agent-workflows Agent instructions, AI-assisted workflow docs, or bot-maintained guidance. area: config Global/default configuration and settings. area: hashing Hashing, key generation, argument handling, or signatures. area: tests Test suite, fixtures, coverage, or local test scripts. bug enhancement impact: performance Performance, scalability, or benchmark impact. outcome: merged Merged into the repository. source: copilot Opened by GitHub Copilot coding agent. testing and development type: bugfix Fixes a user-visible bug or regression. type: feature Adds or materially changes user-facing functionality. type: perf Performance optimization or benchmark-driven change. type: tests Tests, fixtures, test coverage, or test data. labels Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

AI & Agents area: agent-workflows Agent instructions, AI-assisted workflow docs, or bot-maintained guidance. area: config Global/default configuration and settings. area: hashing Hashing, key generation, argument handling, or signatures. area: tests Test suite, fixtures, coverage, or local test scripts. bug enhancement impact: performance Performance, scalability, or benchmark impact. outcome: merged Merged into the repository. source: copilot Opened by GitHub Copilot coding agent. testing and development type: bugfix Fixes a user-visible bug or regression. type: feature Adds or materially changes user-facing functionality. type: perf Performance optimization or benchmark-driven change. type: tests Tests, fixtures, test coverage, or test data.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants