Skip to content

fix: batch-3 - verifier backward compat and thread-safe shim#324

Open
saurabhhhcodes wants to merge 1 commit into
AnkanMisra:mainfrom
saurabhhhcodes:fix/MicroAI-Paygate-batch-3
Open

fix: batch-3 - verifier backward compat and thread-safe shim#324
saurabhhhcodes wants to merge 1 commit into
AnkanMisra:mainfrom
saurabhhhcodes:fix/MicroAI-Paygate-batch-3

Conversation

@saurabhhhcodes

@saurabhhhcodes saurabhhhcodes commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Batch 3 Bug Fixes

Bug 1: Missing backward compatibility in isVerifierBusinessRejection

  • File: gateway/errors.go
  • Issue: isVerifierBusinessRejection only checked ErrorCode field but did not check backward-compatible Error prefixes (E007, E008, E009) like verifierFailureResponse does
  • Impact: Old verifier responses without error_code field would NOT be recognized as business rejections, instead treated as infrastructure errors (returned as 502 Bad Gateway instead of proper 400/409 status)

Bug 2: Data race in responseWriterShim methods

  • File: gateway/middleware.go
  • Issue: Written() and Size() methods accessed bufferedWriter fields without holding the mutex
  • Fix: Added RLock/RUnlock to both methods for thread safety
  • Impact: Potential data race under concurrent access

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of legacy verifier rejection codes to ensure they are classified correctly.
    • Improved reliability when reading buffered response status and size during concurrent request processing.

Note

Fix verifier backward compatibility and thread-safe shim reads in gateway

  • Extends isVerifierBusinessRejection in errors.go to handle responses missing ErrorCode by checking the Error string for E007, E008, or E009 prefixes, maintaining backward compatibility with older verifier responses.
  • Adds read locks (mu.RLock/RUnlock) to Written() and Size() in middleware.go to prevent data races on concurrent reads of responseWriterShim state.

Macroscope summarized b2fecdd.

@vercel

vercel Bot commented Jul 26, 2026

Copy link
Copy Markdown

Someone is attempting to deploy a commit to the ankanmisra's projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the go Pull requests that update go code label Jul 26, 2026
@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The verifier rejection classifier now supports legacy error prefixes for unrecognized codes. Response writer status and size reads now use the buffered writer’s read lock.

Changes

Verifier error classification

Layer / File(s) Summary
Legacy verifier error fallback
gateway/errors.go
isVerifierBusinessRejection checks legacy E007, E008, and E009 prefixes after recognized error-code handling.

Response writer synchronization

Layer / File(s) Summary
Locked response writer state reads
gateway/middleware.go
Written() and Size() read buffered writer state while holding its read lock.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: ankanmisra

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the bugs but omits most required template sections like type, affected areas, verification, and checklist. Add the missing template sections, including type of change, affected areas, checklist, exact verification commands, and reviewer notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the two main fixes in the PR.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (2.12.2)

level=error msg="[linters_context] typechecking error: pattern ./...: directory prefix . does not contain main module or its selected dependencies"


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@gateway/errors.go`:
- Around line 78-83: The legacy-prefix fallback in the verifier error handling
needs regression coverage. Add table-driven cases in the relevant gateway error
test for responses beginning with E007, E008, and E009, plus nonmatching errors
and nil responses, asserting the expected business-rejection result; then run go
test -v ./... and go vet ./... from gateway.

In `@gateway/middleware.go`:
- Around line 245-246: Add a concurrency regression test targeting
responseWriterShim.Written and responseWriterShim.Size, running those
synchronized reads concurrently with Write and WriteString operations and
asserting safe, consistent behavior without races or panics. Place the test with
the gateway tests and validate it using gofmt, go test -v ./..., and go vet
./... from the gateway directory.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 98da2a92-3c0c-405a-9d7e-c7212b24e9e5

📥 Commits

Reviewing files that changed from the base of the PR and between 76ed9ff and b2fecdd.

📒 Files selected for processing (2)
  • gateway/errors.go
  • gateway/middleware.go

Comment thread gateway/errors.go
Comment on lines +78 to +83
// Backward compatibility for older verifier responses without error_code.
if strings.HasPrefix(verifyResp.Error, "E007") ||
strings.HasPrefix(verifyResp.Error, "E008") ||
strings.HasPrefix(verifyResp.Error, "E009") {
return true
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Add regression tests for the legacy-prefix fallback.

This changes the business-rejection contract, but no tests accompany the change. Add cases in gateway/errors_test.go for E007, E008, and E009 prefixes, plus nonmatching and nil responses. Validate with go test -v ./... and go vet ./... from gateway.

As per coding guidelines, Go behavioral changes must add or update tests, and Gateway changes require the stated validation.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/errors.go` around lines 78 - 83, The legacy-prefix fallback in the
verifier error handling needs regression coverage. Add table-driven cases in the
relevant gateway error test for responses beginning with E007, E008, and E009,
plus nonmatching errors and nil responses, asserting the expected
business-rejection result; then run go test -v ./... and go vet ./... from
gateway.

Source: Coding guidelines

Comment thread gateway/middleware.go
Comment on lines +245 to +246
func (rws *responseWriterShim) Written() bool { rws.bw.mu.RLock(); defer rws.bw.mu.RUnlock(); return rws.bw.wrote }
func (rws *responseWriterShim) Size() int { rws.bw.mu.RLock(); defer rws.bw.mu.RUnlock(); return rws.bw.buf.Len() }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== file size =="
wc -l gateway/middleware.go || true

echo "== locate responseWriterShim =="
rg -n "responseWriterShim|type .*bw|func \\(rws \\*responseWriterShim\\)" gateway/middleware.go || true

echo "== relevant middleware.go lines =="
sed -n '200,270p' gateway/middleware.go || true

echo "== write helpers/mutators around buffer =="
rg -n -C 3 "buf|wrote|Written\\(|Size\\(|WriteString|Write\\(" gateway/middleware.go || true

echo "== responseWriterShim tests/usages =="
rg -n "responseWriterShim|Written\\(|Size\\(|concurrent|goroutine" gateway -g '*.go' || true

echo "== git diff stat/name =="
git diff --stat -- gateway/middleware.go || true
git diff --unified=80 -- gateway/middleware.go | sed -n '1,220p' || true

echo "== gateway module/tests status =="
test -f gateway/go.mod && echo "gateway/go.mod exists" || echo "gateway/go.mod missing"
git ls-files gateway | sed -n '1,120p'

echo "== validate environment =="
command -v go || true
go version 2>/dev/null || true

Repository: AnkanMisra/MicroAI-Paygate

Length of output: 15654


Add a concurrency regression test for the synchronized reads.

Written() and Size() now acquire RLock before reading shared bufferedWriter state, while Write/WriteString use exclusive locks. Add a test that exercises concurrent writes with Written()/Size() reads and run the gateway Go checks: gofmt -w ., go test -v ./..., and go vet ./... from gateway.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@gateway/middleware.go` around lines 245 - 246, Add a concurrency regression
test targeting responseWriterShim.Written and responseWriterShim.Size, running
those synchronized reads concurrently with Write and WriteString operations and
asserting safe, consistent behavior without races or panics. Place the test with
the gateway tests and validate it using gofmt, go test -v ./..., and go vet
./... from the gateway directory.

Source: Coding guidelines

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant