Skip to content

Commit

Permalink
fix: don't panic when unordered operations are processed by scheduler
Browse files Browse the repository at this point in the history
When the scheduler processes multiple operations, it stores then
in a slice according to their `Index` field:
```go
store[op.Id][op.Index] = op
```

To ensure the slice has the capacity to store the item, there's an
allocation block, right before:
```go
if len(store[op.Id]) <= op.Index {
    store[op.Id] = append(store[op.Id], op)
}
```

However, the code above panics with "index out of range" if the delta
between the slice length and the operation index is greater than 1 --
for instance, if the slice is empty and the first operation has index 2.
To fix it, we must perform the `append` multiple times:
```go
for len(store[op.Id]) <= op.Index {
    store[op.Id] = append(store[op.Id], op)
}
```
  • Loading branch information
gustavogama-cll committed Feb 1, 2025
1 parent 3676476 commit 78d264c
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/timelock/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (tw *scheduler) runScheduler(ctx context.Context) <-chan struct{} {

case op := <-tw.add:
tw.mu.Lock()
if len(tw.store[op.Id]) <= int(op.Index.Int64()) {
for len(tw.store[op.Id]) <= int(op.Index.Int64()) {
tw.store[op.Id] = append(tw.store[op.Id], op)
}
tw.store[op.Id][op.Index.Int64()] = op
Expand Down

0 comments on commit 78d264c

Please sign in to comment.