Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relegate the on-close for emergencies #1175

Merged
merged 4 commits into from
Jul 5, 2022
Merged

Relegate the on-close for emergencies #1175

merged 4 commits into from
Jul 5, 2022

Conversation

cbm755
Copy link
Collaborator

@cbm755 cbm755 commented Jul 4, 2022

@alexvong1995 how about this?

We do the waiting and verbosity etc in the explicit reset case. Then the onClose stuff becomes kind of an emergency catch-all for situations (like "pkg unload"). To me, this makes it clear, that its pkg unload's fault for not giving us a hook ;-)

Alex Vong and others added 2 commits July 4, 2022 12:32
This warning

--8<---------------cut here---------------start------------->8---
warning: onCleanup: error caught while executing cleanup function:
'python_ipc_popen2_reset' undefined near line 78, column 30
  "'python_ipc_popen2_reset' undefined"
--8<---------------cut here---------------end--------------->8---

is caused by "pkg unload symbolic".

We fix it by closing file descriptors and waitpid directly in the
cleanup function instead of using private/python_ipc_popen2_reset.

Fixes #1160.

* inst/private/python_ipc_popen2.m: Close file descriptors and waitpid
directly in the cleanup function.
* inst/private/python_ipc_popen2_reset.m: Remove file.
then (@(varagin) ...
[is_valid_file_id(fin) && fclose(fin), ...
is_valid_file_id(fout) && fclose(fout)], ...
@(varargin) waitpid(pid)));
Copy link
Collaborator Author

@cbm755 cbm755 Jul 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how big a crime is closing here without calling waitpid? Then we would not need the then...

Note: in the explicit reset case (and a glorious future where pkg unload has a hook), we will have waited on pid earlier and all of these variables will be empty.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we don't waitpid, we'll be left with a zombie process python3 <defunct>, because fclose (fin) will terminate the child process (i.e. python3). On the other hand, if we waitpid before fclose (fin), it will block, because waitpid will wait for the child process (i.e. python3) to terminate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@cbm755
Copy link
Collaborator Author

cbm755 commented Jul 5, 2022

So can you look over this one? It builds on top of your commit, but restores things to be closer to how they were before @mtmiller split off python_ipc_popen2_reset.m in dfe236d

So in the explicit reset situation, the onCleanup code becomes a no-op.

Copy link
Collaborator

@alexvong243f alexvong243f left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise LGTM!

fout = [];
A = A && (t == 0);
end
emergency_cleanup = []; % note: triggers emergency file-close
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about this line. Why do we need to "trigger emergency file-close"? IIUC we've just cleaned things up above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its going to get triggered eventually. Might as well trigger it now, when I know it is an no-op.

I guess I'm vague uncomfortable with this split: here on line 63 we close fout, let's say its 24. My understanding of how anonymous functions work is that the onCleanup still has "24" hardcoded in it (it has no idea fout has been replaces with []).

Some other fopen call later reuses 24.

Now our ticking timebomb onCleanup goes off and closes that 24. I don't know if that can really happen but the idea that it might means I don't like that onCleanup lying around. Let it go off right now while its harmless.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe I should have a comment that gives that idea: % trigger while we know it is a no-op?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, this should make things clearer

inst/private/python_ipc_popen2.m Outdated Show resolved Hide resolved
Just in case the onCleanup triggers early and they have been closed
or something.
@cbm755 cbm755 merged commit 126a36d into main Jul 5, 2022
@cbm755 cbm755 deleted the onclose branch July 5, 2022 08:27
@mmuetzel
Copy link
Member

mmuetzel commented Jul 5, 2022

its pkg unload's fault for not giving us a hook

Not sure what exactly you need. But the PKG_DEL scripts might do something similar. See: https://docs.octave.org/latest/Creating-Packages.html
Essentially, these scripts are executed whenever the path containing them is removed from Octave's load path. That is happening on pkg unload. (But admittedly, it's not a real hook but just a script.)
Maybe you could use (mlocked) functions that keep some values stored in persistent variables if you need to access them from the PKG_DEL script?

As a side note (and I don't know if that matters here): Persistent variables are cleared whenever a function gets reparsed. There are no guarantees when that is happening (unless you mlock that function).

@cbm755
Copy link
Collaborator Author

cbm755 commented Jul 5, 2022

Persistent variables are cleared whenever a function gets reparsed. There are no guarantees when that is happening (unless you mlock that function).

Thanks @mmuetzel. As long as the onCleanup triggers, then I think that's fine and how we want it. The onCleanup closes the file handles. And then next symbolic command would rebuild the pipe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants