Skip to content

🐛 bug: Make idempotency MemoryLock safe for zero-value use#4371

Merged
ReneWerner87 merged 2 commits into
mainfrom
copilot/fix-memorylock-nil-map-write
May 28, 2026
Merged

🐛 bug: Make idempotency MemoryLock safe for zero-value use#4371
ReneWerner87 merged 2 commits into
mainfrom
copilot/fix-memorylock-nil-map-write

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 28, 2026

Description

idempotency.MemoryLock panicked when used as a zero value because its internal keys map was nil on first Lock(). This change makes zero-value usage safe without changing the existing constructor-based path.

  • Zero-value safety

    • Lazily initialize MemoryLock.keys inside Lock() before first use.
    • Preserve existing lock lifecycle and cleanup behavior for constructed and zero-value instances.
  • Regression coverage

    • Add focused test coverage for:
      • var l idempotency.MemoryLock
      • first Lock() / Unlock()
      • unlocking a missing key after zero-value initialization
  • Usage example

    var l idempotency.MemoryLock
    
    if err := l.Lock("key"); err != nil {
        return err
    }
    defer l.Unlock("key")

Changes introduced

  • Benchmarks: No benchmark changes.
  • Documentation Update: No documentation changes required.
  • Changelog/What's New: Zero-value MemoryLock no longer panics on first use.
  • Migration Guide: No migration needed.
  • API Alignment with Express: Not applicable.
  • API Longevity: Preserves the exported type and constructor while making the zero value safe, reducing accidental misuse without API changes.
  • Examples: Added a zero-value usage example in this description.

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • Enhancement (improvement to existing features and functionality)
  • Documentation update (changes to documentation)
  • Performance improvement (non-breaking change which improves efficiency)
  • Code consistency (non-breaking change which improves code reliability and robustness)

Checklist

Before you submit your pull request, please make sure you meet these requirements:

  • Followed the inspiration of the Express.js framework for new functionalities, making them similar in usage.
  • Conducted a self-review of the code and provided comments for complex or critical parts.
  • Updated the documentation in the /docs/ directory for Fiber's documentation.
  • Added or updated unit tests to validate the effectiveness of the changes or new features.
  • Ensured that new and existing unit tests pass locally with the changes.
  • Verified that any new dependencies are essential and have been agreed upon by the maintainers/community.
  • Aimed for optimal performance with minimal allocations in the new code.
  • Provided benchmarks for the new code to analyze and improve upon.

Commit formatting

Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md

Copilot AI changed the title [WIP] Fix panic on zero-value usage of MemoryLock Make idempotency MemoryLock safe for zero-value use May 28, 2026
Copilot finished work on behalf of gaby May 28, 2026 04:32
Copilot AI requested a review from gaby May 28, 2026 04:32
@gaby gaby changed the title Make idempotency MemoryLock safe for zero-value use 🐛 bug: Make idempotency MemoryLock safe for zero-value use May 28, 2026
@gaby gaby marked this pull request as ready for review May 28, 2026 04:36
@gaby gaby requested a review from a team as a code owner May 28, 2026 04:36
@ReneWerner87 ReneWerner87 added this to v3 May 28, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone May 28, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes middleware/idempotency.MemoryLock safe to use as a zero value by lazily initializing its internal key map on first lock acquisition.

Changes:

  • Lazily initializes MemoryLock.keys inside Lock().
  • Adds regression coverage for zero-value MemoryLock lock/unlock behavior and missing-key unlock after initialization.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
middleware/idempotency/locker.go Adds lazy map initialization to support zero-value MemoryLock usage.
middleware/idempotency/locker_test.go Adds a zero-value regression subtest for MemoryLock.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.35%. Comparing base (e5e93e6) to head (b4476cb).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4371      +/-   ##
==========================================
- Coverage   91.39%   91.35%   -0.05%     
==========================================
  Files         132      132              
  Lines       13098    13100       +2     
==========================================
- Hits        11971    11967       -4     
- Misses        710      715       +5     
- Partials      417      418       +1     
Flag Coverage Δ
unittests 91.35% <100.00%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ReneWerner87 ReneWerner87 merged commit bf86a05 into main May 28, 2026
29 checks passed
@ReneWerner87 ReneWerner87 deleted the copilot/fix-memorylock-nil-map-write branch May 28, 2026 05:28
@github-project-automation github-project-automation Bot moved this to Done in v3 May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

🐛 [Bug]: MemoryLock panics on zero-value usage — nil map write

4 participants