Skip to content

Commit 3de79a3

Browse files
authored
Add cleanup hook to nrepl exit (#77)
Then, when the nrepl closes, we cleanup connection-related state to avoid broken channel errors
1 parent c956454 commit 3de79a3

File tree

4 files changed

+58
-16
lines changed

4 files changed

+58
-16
lines changed

lua/acid/connections.lua

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,33 @@ connections.add = function(addr)
2424
return ulid
2525
end
2626

27-
connections.remove = function(addr)
28-
local ix
29-
30-
-- Remove current connections first
31-
-- Then remove remaining
32-
for pwd, v in pairs(connections.current) do
33-
if v[2] == addr[2] and v[1] == addr[1] then
34-
ix = connections.current[pwd]
35-
connections.store[ix] = nil
36-
connections.current[pwd] = nil
27+
connections.remove = function(pwd, addr)
28+
-- If removed address is current to a pwd
29+
local key = connections.current[pwd]
30+
local conn = connections.store[key]
31+
32+
-- Double-check if address is correct
33+
if key ~= nil and conn ~= nil and conn[2] == addr[2] and conn[1] == addr[1] then
34+
-- Then remove it from current pwd
35+
connections.current[pwd] = nil
36+
-- Remove it's definition
37+
connections.store[key] = nil
38+
39+
-- And remove all other addresses that point to it
40+
for ix, v in pairs(connections.current) do
41+
if v == key then
42+
connections.current[ix] = nil
43+
end
3744
end
38-
end
39-
40-
for i, v in ipairs(connections.store) do
41-
if v[2] == addr[2] and v[1] == addr[1] then
42-
connections.store[i] = nil
45+
else
46+
-- Else, remove it from the connections if no address points to it
47+
for i, v in pairs(connections.store) do
48+
if v[2] == addr[2] and v[1] == addr[1] then
49+
connections.store[i] = nil
50+
end
4351
end
4452
end
53+
4554
end
4655

4756
--- Elects selected connection as primary (thus default) for a certain address

lua/acid/nrepl.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ nrepl.start = function(obj)
130130
cmd , {
131131
on_stdout = "AcidJobHandler",
132132
on_stderr = "AcidJobHandler",
133+
on_exit = "AcidJobCleanup",
133134
cwd = pwd
134135
}
135136
})
@@ -165,10 +166,21 @@ nrepl.stop = function(obj)
165166

166167
nvim.nvim_call_function("jobstop", {nrepl.cache[pwd].job})
167168
connections.unselect(pwd)
168-
connections.remove(nrepl.cache[pwd].addr)
169+
connections.remove(pwd, nrepl.cache[pwd].addr)
169170
nrepl.cache[obj.pwd] = nil
170171
end
171172

173+
nrepl.cleanup = function(job_id)
174+
nrepl.handle._store[job_id] = nil
175+
local pwd = utils.search(nrepl.cache, function(obj)
176+
return obj.job == job_id
177+
end)
178+
179+
if pwd ~= nil then
180+
nrepl.stop{pwd = pwd}
181+
end
182+
end
183+
172184
nrepl.handle = {
173185
_store = {},
174186
stdout = function(dt, ch)

lua/acid/utils.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,22 @@ utils.find = function(coll, val)
5151
return false
5252
end
5353

54+
utils.search = function(coll, pred)
55+
for key, v in pairs(coll) do
56+
if pred(v, key) then
57+
return key
58+
end
59+
end
60+
for ix, v in ipairs(coll) do
61+
if pred(v, ix) then
62+
return ix
63+
end
64+
end
65+
return nil
66+
end
67+
68+
69+
5470
utils.map = function(tbl, fn)
5571
local new = {}
5672

plugin/acid.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ function! AcidJobHandler(id, data, stream)
8787
call luaeval('require("acid.nrepl").handle[_A[1]](_A[2], _A[3])', [a:stream, a:data, a:id])
8888
endfunction
8989

90+
function! AcidJobCleanup(id, data, _)
91+
call luaeval('require("acid.nrepl").cleanup(_A[1])', [a:id])
92+
endfunction
93+
94+
9095
map <Plug>(acid-interrupt) <Cmd>lua require("acid.features").interrupt()<CR>
9196
map <Plug>(acid-go-to) <Cmd>lua require("acid.features").go_to()<CR>
9297
map <Plug>(acid-go-to) <Cmd>lua require("acid.features").go_to()<CR>

0 commit comments

Comments
 (0)