Skip to content

Commit

Permalink
fix: use goroutine progress instead of state to determine deadlock (#24)
Browse files Browse the repository at this point in the history
* feat: track if a goroutine has made progress

This is done by keeping track of what is the last instruction that is
executed by the goroutine. `progress = prev_instruction !==
curr_instruction`

* fix: check goroutine progress during deadlock detection
  • Loading branch information
shenyih0ng authored Apr 7, 2024
1 parent 8fd8dd9 commit f66271e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/go-slang/goroutine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export class GoRoutine {
private context: Context
private scheduler: Scheduler

// used to determine if the goroutine made progress in the last tick
public progress: boolean = false
private prevInst: Instruction | null = null

public state: GoRoutineState
public isMain: boolean

Expand All @@ -114,6 +118,9 @@ export class GoRoutine {
Result.ok(C.isEmpty() ? GoRoutineState.Exited : GoRoutineState.Running)

this.state = nextState.isSuccess ? nextState.unwrap() : GoRoutineState.Exited
this.progress = this.prevInst !== inst
this.prevInst = inst

return nextState
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/go-slang/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export class Scheduler {

@benchmark('Scheduler::run')
public run(): void {
let numConsecAllBlocks = 0
// the number of consecutive time quanta where all routines made no progress
let numConsecNoProgress = 0

while (this.routines.length && numConsecAllBlocks < this.routines.length) {
while (this.routines.length && numConsecNoProgress < this.routines.length) {
const [routine, timeQuanta] = this.routines.shift() as [GoRoutine, TimeQuanta]

let remainingTime = timeQuanta
Expand All @@ -52,10 +53,8 @@ export class Scheduler {

this.schedule(routine)

const hasRunningRoutines = this.routines.some(
([{ state }]) => state === GoRoutineState.Running
)
numConsecAllBlocks = hasRunningRoutines ? 0 : numConsecAllBlocks + 1
const hasProgress = this.routines.some(([{ progress }]) => progress)
numConsecNoProgress = hasProgress ? 0 : numConsecNoProgress + 1
}

// if we reach here, all routines are blocked
Expand Down

0 comments on commit f66271e

Please sign in to comment.