Fix: Add transaction protection for subtask creation#368
Open
lxcxjxhx wants to merge 4 commits into
Open
Conversation
- Fix double timeout padding in Handle() method that inflated user-requested timeouts by 5 seconds - Fix incorrect log type in WriteFile() method (stdin -> stdout) for proper log stream classification - Fix incomplete file read in ReadFile() method by using io.ReadFull instead of single Read() call These bugs affected command execution timeout handling, log type correctness, and file read completeness. All fixes are minimal, targeted, and maintain backward compatibility while improving correctness.
- Remove defer close(q.queue) from reader() to prevent double close - Add explicit close(q.queue) in Stop() after cancel() to ensure workers exit - This fixes goroutine leak when Stop() is called while workers are still processing The issue: when Stop() calls cancel(), the reader() might not exit immediately, and workers waiting on q.queue channel would block forever, causing goroutine leak.
- Pass *sql.DB through controller chain to enable transactions - Wrap GenerateSubtasks in transaction with proper rollback - Wrap RefineSubtasks delete+create in single transaction - Ensures atomicity and prevents partial subtask creation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
GenerateSubtasksandRefineSubtasksfunctions insubtasks.gocreate multiple subtasks in a loop without transaction protection. If any subtask creation fails midway, the database is left in an inconsistent state with partial subtasks created.Solution
Added transaction support by passing
*sql.DBthrough the controller chain and wrapping subtask operations in transactions:Architecture changes:
RawDB *sql.DBfield toFlowContext(context.go)rawDB *sql.DBfield toflowWorkerCtx(flow.go)rawDB *sql.DBfield toflowControllerand updatedNewFlowControllersignature (flows.go)main.goto pass the database connection to the flow controllerTransaction implementation:
GenerateSubtasks: Wraps allCreateSubtaskcalls in a transaction with proper rollback on errorRefineSubtasks: Wraps delete and create operations in a single transaction to ensure atomicityChanges
backend/cmd/pentagi/main.go: PassdbtoNewFlowControllerbackend/pkg/controller/context.go: AddRawDB *sql.DBtoFlowContextbackend/pkg/controller/flow.go: AddrawDBtoflowWorkerCtxand propagate toFlowContextbackend/pkg/controller/flows.go: AddrawDBtoflowController, update constructor signaturebackend/pkg/controller/subtasks.go: Implement transaction-protected subtask operationsTesting
go buildImpact
This fix ensures data consistency when creating or refining subtasks. If any operation fails, the entire transaction is rolled back, preventing partial subtask creation that could lead to inconsistent task states.
The original code had TODO comments acknowledging this issue:
// TODO: change it to insert subtasks in transaction// TODO: change it to insert subtasks in transaction and union it with delete onesThis PR resolves both TODOs.