Channels: Deadlocks
● Advanced2 min
If every task waits and none can go on, the program stops with a clear panic instead of hanging forever.
fn main() {
const (send, receive) = Channel<Int>(capacity: 1)
send.put(1)
for n in receive {
print(n)
}
}The program ran, then stopped with a panic:
kavach run deadlock.kav
1panic: every task is waiting for another one (a deadlock)
The channel is never closed, so the loop waits for a value that can't come. Kavach sees that no task can go on, and says so. The fix: send.close() after the last put.