Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@kevwan
Copy link
Contributor

@kevwan kevwan commented Dec 7, 2025

Description

Fixes #5314

This PR addresses the issues reported in the TimingWheel implementation:

Changes Made

  1. Added runner.Wait() in drainAll - This is the critical bug fix

    • The function was returning immediately without waiting for scheduled tasks to complete
    • This could cause race conditions and unexpected behavior when drain operations were expected to be complete
    • Now properly waits for all tasks to finish execution
  2. Copy loop variable in runTasks - Code quality improvement

    • Changed from tasks[i].key, tasks[i].value to task.key, task.value with a local copy
    • While the current code works (since RunSafe executes synchronously), this change:
      • Follows Go best practices
      • Makes the code more resilient to future changes
      • Improves code clarity and readability

Testing

  • All existing TimingWheel tests pass
  • Added comprehensive test suite in timingwheel_issue5314_test.go to verify:
    • Drain operations with multiple concurrent tasks
    • Task execution with proper value capture
    • Race condition scenarios

Analysis Summary

The closure variable captures themselves were not causing bugs in the current implementation (task is redeclared in loop body, RunSafe executes synchronously), but the missing runner.Wait() was a real bug that needed fixing. The suggested improvements enhance code quality and follow Go idioms.

Copilot AI review requested due to automatic review settings December 7, 2025 03:21
@kevwan kevwan force-pushed the fix/timingwheel-wait-and-closure branch from 8a6eb61 to 5aea2d3 Compare December 7, 2025 03:26
Copy link
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 fixes a critical bug in the TimingWheel's drainAll function and improves code clarity in the runTasks function. The main issue was that drainAll returned immediately without waiting for scheduled tasks to complete, potentially causing race conditions.

  • Added missing runner.Wait() call in drainAll to ensure all drain tasks complete before returning
  • Improved code clarity in runTasks by copying the loop variable to follow Go best practices
  • Added comprehensive test suite to verify drain operations and task execution under various conditions

Reviewed changes

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

File Description
core/collection/timingwheel.go Fixed critical bug by adding runner.Wait() in drainAll function; improved loop variable capture pattern in runTasks for better code clarity
core/collection/timingwheel_issue5314_test.go Added comprehensive test suite with three tests covering drain wait behavior, multiple task execution, and concurrent stress testing scenarios

Fixes zeromicro#5314

Changes:
- Add runner.Wait() in drainAll to ensure all scheduled tasks complete before returning
- Copy loop variable in runTasks for better defensive programming and code clarity

The missing Wait() call was a real bug that could cause race conditions when drainAll
returned before all scheduled tasks completed. The loop variable copy in runTasks
follows Go best practices and makes the code more resilient to future changes.

All existing tests pass with these changes.

Signed-off-by: kevin <[email protected]>
@kevwan kevwan force-pushed the fix/timingwheel-wait-and-closure branch from 5aea2d3 to e29febc Compare December 7, 2025 03:27
@codecov
Copy link

codecov bot commented Dec 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kevwan kevwan merged commit 7b23f73 into zeromicro:master Dec 7, 2025
6 checks passed
@kevwan kevwan deleted the fix/timingwheel-wait-and-closure branch December 7, 2025 03:37
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.

TimingWheel has multiple logic issues, with closure capture and missing Wait being the most severe!

1 participant