Skip to content

Commit

Permalink
Merge pull request #1175 from cbm755/onclose
Browse files Browse the repository at this point in the history
Relegate the on-close for emergencies
  • Loading branch information
cbm755 committed Jul 5, 2022
2 parents 90ed62f + d300ea6 commit 126a36d
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions inst/private/python_ipc_popen2.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

function [A, info] = python_ipc_popen2(what, cmd, varargin)

persistent cleanup fin fout pid
persistent emergency_cleanup fin fout pid

py_startup_timeout = 30; % seconds

Expand All @@ -44,11 +44,29 @@
info = [];

if (strcmp(what, 'reset'))
cleanup = []; % triggers a call to reset function
if (~ isempty (pid))
if (verbose)
disp ('Closing the Python communications link.')
end
end
A = true;
if (is_valid_file_id (fin))
% produces a single newline char: not sure why
t = fclose (fin);
fin = [];
waitpid (pid);
pid = [];
A = A && (t == 0);
end
if (is_valid_file_id (fout))
t = fclose (fout);
fout = [];
A = A && (t == 0);
end
emergency_cleanup = []; % trigger while we know it is a no-op
fin = [];
fout = [];
pid = [];
A = true;
return
end

Expand Down Expand Up @@ -76,12 +94,12 @@
end

then = @(f, g) g (f ());
% this will be called when we `clear cleanup`: avoids dangling file handles
cleanup = onCleanup (@() ...
% will be called when we `clear functions`: avoids dangling file handles
emergency_cleanup = onCleanup (@() ...
then (@(varagin) ...
[is_valid_file_id(fin) && fclose(fin), ...
is_valid_file_id(fout) && fclose(fout)], ...
@(varargin) waitpid(pid)));
@(varargin) ~isempty(pid) && waitpid(pid)));
headers = python_header();
fputs (fin, headers);
fprintf (fin, '\n\n');
Expand Down

0 comments on commit 126a36d

Please sign in to comment.