Skip to content

Commit

Permalink
fix: ensure that handlers are emptied after notify_all (nvim-lua#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
NGPONG authored Aug 24, 2023
1 parent f97a076 commit 0dbe561
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lua/plenary/async/control.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ function Condvar:notify_all()
callback()
end

for i = 1, len do
for _ = 1, len do
-- table.remove will ensure that indexes are correct and make "ipairs" safe,
-- which is not the case for "self.handles[i] = nil"
table.remove(self.handles, i)
table.remove(self.handles)
end
end

Expand Down
30 changes: 30 additions & 0 deletions tests/plenary/async/condvar_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,34 @@ describe("condvar", function()

eq(3, counter)
end)

a.it("notify all works multiple times", function()
local condvar = Condvar.new()
local counter = 0

a.run(function()
condvar:wait()
counter = counter + 1
end)

a.run(function()
condvar:wait()
counter = counter + 1
end)

eq(0, counter)

condvar:notify_all()

eq(2, counter)

a.run(function()
condvar:wait()
counter = 0
end)

condvar:notify_all()

eq(0, counter)
end)
end)

0 comments on commit 0dbe561

Please sign in to comment.