Skip to content

Add race condition in inventory service#52

Merged
Chrisbattarbee merged 10 commits intomainfrom
cbattarbee/add-race-condition
Jul 22, 2025
Merged

Add race condition in inventory service#52
Chrisbattarbee merged 10 commits intomainfrom
cbattarbee/add-race-condition

Conversation

@Chrisbattarbee
Copy link
Copy Markdown
Contributor

@Chrisbattarbee Chrisbattarbee commented Jul 22, 2025

Adds a service that introduces a race condition.

[claude-review]

@claude
Copy link
Copy Markdown

claude bot commented Jul 22, 2025

Claude finished @Chrisbattarbee's task —— View job


Code Review: Add race condition in inventory service

I've completed my review of this PR that adds an inventory service with intentional race conditions. Here's my comprehensive feedback:

Todo List

  • Read project documentation and understand context
  • Review inventory service implementation
  • Assess code quality and best practices
  • Evaluate security implications
  • Check test coverage
  • Review integration with other services
  • Provide comprehensive feedback

Summary

This PR successfully introduces an inventory service with intentional race conditions for debugging practice, which aligns with the project's purpose. However, there are several improvements needed for production-readiness and safety.

Code Quality and Best Practices

✅ Positive aspects:

  • Well-structured Go service following standard patterns
  • Proper use of OpenTelemetry for distributed tracing
  • Structured logging with trace context propagation
  • Clear REST API design
  • Good separation of concerns

⚠️ Issues found:

  1. Delayed initialization creates startup race (inventory-service/main.go:55-58)

    • The reserved map is initialized asynchronously after 100ms delay
    • Any request before initialization completes will panic with nil pointer dereference
    • Recommendation: Initialize synchronously or add nil checks in handlers
  2. Missing error handling for nil map (inventory-service/main.go:137,196,264)

    • No checks if store.reserved is nil before access
    • Recommendation: Add defensive nil checks or ensure initialization completes before serving
  3. Inconsistent locking patterns

    • inventory map properly uses RWMutex
    • reserved map has no locking at all
    • Recommendation: Document this is intentional for debugging scenarios

Security Concerns

  1. Intentional race conditions pose risks:

    • While appropriate for a debugging demo, these vulnerabilities could be exploited if deployed accidentally
    • Recommendation: Add clear warnings in README and code comments marking this as UNSAFE FOR PRODUCTION
  2. Panic on data corruption (inventory-service/main.go:227-228)

    • Service crashes when detecting inconsistent state
    • Recommendation: Consider logging critical errors without panicking for better observability
  3. No input validation limits:

    • No max quantity limits on reservation requests
    • Could lead to integer overflow with large values
    • Recommendation: Add reasonable bounds checking

Performance Considerations

  1. Race conditions cause performance degradation:

    • Concurrent modifications without synchronization lead to cache line bouncing
    • Data corruption requires service restarts
    • Recommendation: Document expected performance characteristics
  2. Random delays affect latency (inventory-service/main.go:181,199):

    • Added delays simulate real-world conditions but impact performance
    • Recommendation: Make delays configurable via environment variables

Test Coverage

✅ Good test approach:

  • Python test script effectively demonstrates race conditions
  • Concurrent load generation with clear metrics
  • Shows both successful and failed scenarios

⚠️ Missing tests:

  • No Go unit tests for the service
  • No integration tests with other services
  • No tests for edge cases (nil map, negative quantities)
  • Recommendation: Add main_test.go with basic handler tests

Integration Review

  1. Gateway integration looks correct:

    • Proper error handling when inventory service fails
    • Continues checkout even if reservation fails (resilient design)
  2. Service discovery works well:

    • Consistent port configuration (8085)
    • Proper environment variable setup in docker-compose
    • Kubernetes manifests correctly configured
  3. Missing release mechanism:

    • Gateway reserves inventory but never releases on checkout failure
    • Could lead to inventory exhaustion
    • Recommendation: Implement cleanup/timeout mechanism

Recommendations

  1. Add prominent warnings:

    // WARNING: This service contains INTENTIONAL RACE CONDITIONS for debugging practice
    // DO NOT use this code in production environments
  2. Create a feature flag to enable/disable race conditions:

    ENABLE_RACE_CONDITIONS=true  # Default to false
  3. Add metrics for race condition detection:

    • Counter for data corruption events
    • Histogram for reservation inconsistencies
  4. Document the debugging scenarios in DEBUGGING_SCENARIOS.md (which was removed?)

  5. Consider adding Go's race detector in tests:

    go test -race ./...

Conclusion

This PR successfully achieves its goal of adding a service with race conditions for debugging practice. The implementation is realistic and will provide good learning opportunities. However, additional safety measures and documentation are needed to prevent accidental production deployment.

Verdict: Approve with changes - The core functionality is good, but please address the safety and documentation concerns before merging.

@Chrisbattarbee Chrisbattarbee merged commit 8e5a359 into main Jul 22, 2025
1 check passed
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.

1 participant