Abort python processes on Vim exit

You'd think a scripting language could handle portably checking if a pid
is still running but no.
This commit is contained in:
Tim Pope 2014-01-07 23:07:31 -05:00
parent 280b8d09b5
commit 15be24fc82
2 changed files with 17 additions and 7 deletions

View File

@ -164,12 +164,16 @@ function! s:extract_last_stacktrace(nrepl) abort
return stacktrace return stacktrace
endfunction endfunction
let s:keepalive = tempname()
call writefile([getpid()], s:keepalive)
function! s:nrepl_dispatch(command, ...) dict abort function! s:nrepl_dispatch(command, ...) dict abort
let in = 'python' let in = 'python'
\ . ' ' . s:shellesc(s:python_dir.'/nrepl_fireplace.py') \ . ' ' . s:shellesc(s:python_dir.'/nrepl_fireplace.py')
\ . ' ' . s:shellesc(a:command) \ . ' ' . s:shellesc(a:command)
\ . ' ' . s:shellesc(self.host) \ . ' ' . s:shellesc(self.host)
\ . ' ' . s:shellesc(self.port) \ . ' ' . s:shellesc(self.port)
\ . ' ' . s:shellesc(s:keepalive)
\ . ' ' . join(map(copy(a:000), 's:shellesc(v:val)'), ' ') \ . ' ' . join(map(copy(a:000), 's:shellesc(v:val)'), ' ')
let out = system(in) let out = system(in)
if !v:shell_error if !v:shell_error
@ -228,7 +232,7 @@ def fireplace_check():
def fireplace_repl_dispatch(command, *args): def fireplace_repl_dispatch(command, *args):
try: try:
fireplace_let('out', nrepl_fireplace.dispatch(command, vim.eval('self.host'), vim.eval('self.port'), fireplace_check, *args)) fireplace_let('out', nrepl_fireplace.dispatch(command, vim.eval('self.host'), vim.eval('self.port'), fireplace_check, None, *args))
except Exception, e: except Exception, e:
fireplace_let('err', str(e)) fireplace_let('err', str(e))
EOF EOF

View File

@ -59,14 +59,20 @@ def bdecode(f, char=None):
class Connection: class Connection:
def __init__(self, host, port, poll=noop): def __init__(self, host, port, custom_poll=noop, keepalive_file=None):
self.poll = poll self.custom_poll = custom_poll
self.keepalive_file = keepalive_file
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(8) s.settimeout(8)
s.connect((host, int(port))) s.connect((host, int(port)))
s.setblocking(1) s.setblocking(1)
self.socket = s self.socket = s
def poll(self):
self.custom_poll()
if self.keepalive_file and not os.path.exists(self.keepalive_file):
exit(0)
def close(self): def close(self):
return self.socket.close() return self.socket.close()
@ -91,16 +97,16 @@ class Connection:
if 'status' in responses[-1] and 'done' in responses[-1]['status']: if 'status' in responses[-1] and 'done' in responses[-1]['status']:
return responses return responses
def dispatch(command, host, port, poll, *args): def dispatch(command, host, port, poll, keepalive, *args):
conn = Connection(host, port, poll) conn = Connection(host, port, poll, keepalive)
try: try:
return getattr(conn, command)(*args) return getattr(conn, command)(*args)
finally: finally:
conn.close() conn.close()
def main(command, host, port, *args): def main(command, host, port, keepalive, *args):
try: try:
sys.stdout.write(vim_encode(dispatch(command, host, port, noop, *args))) sys.stdout.write(vim_encode(dispatch(command, host, port, noop, keepalive, *args)))
except Exception, e: except Exception, e:
print(e) print(e)
exit(1) exit(1)