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

Fix "Interrupt computations" on Windows #16142

Merged
merged 1 commit into from
Oct 13, 2022

Conversation

jfehrle
Copy link
Member

@jfehrle jfehrle commented Jun 6, 2022

Windows doesn't support signals, so another mechanism was used to support "Interrupt computations" in CoqIDE. On Windows, every process that shares a console (i.e. coqide and all the coqidetop's and worker processes) receives the interrupt. This PR creates an empty file in a temp directory to signal which coqtopide should pay attention to the interrupt. The others will ignore it.

For all major releases back to 8.5pl3, interrupt stops working in a buffer after it's been reset. The original code in coqide_WIN32.c.in dating from 2011 called FreeConsole(), which detaches coqide from the console. Then when you subsequently reset the CoqIDE buffer, creating a new process, the new coqidetop doesn't inherit the console, so it can't be interrupted.

Also, contrary to the documentation, the SetConsoleCtrlHandler appears to affect more than the calling process. Removing that means that coqide will send the signal to itself, so I added code to ignore the signal in that case.

I suspect the bug dates to the original code rather than being a regression. It's possible this worked in 2011 and that the behavior of these Windows calls has changed. I'm on Windows 10.

While I've tested this a lot, it would be nice to get someone with a fresh perspective to try it on Windows in case I missed something.

Fixes: #13550

@jfehrle jfehrle added kind: fix This fixes a bug or incorrect documentation. part: CoqIDE Issues and PRs related to CoqIDE or other IDE features of coq. labels Jun 6, 2022
@jfehrle jfehrle added this to the 8.17+rc1 milestone Jun 6, 2022
@jfehrle jfehrle requested review from a team as code owners June 6, 2022 00:26
@jfehrle jfehrle force-pushed the fix_win_interrupt2 branch 2 times, most recently from 86d2b93 to 011fbdf Compare June 6, 2022 01:31
@jfehrle
Copy link
Member Author

jfehrle commented Jun 6, 2022

The dune file and the setup for the additional OS specific file seem pretty wordy. I'm open to suggestions on how to improve them. Rudi had some suggestions in #16139.

@jfehrle jfehrle force-pushed the fix_win_interrupt2 branch from 011fbdf to a2b5c42 Compare June 6, 2022 02:46
try !interrupter (CoqTop.unixpid coqtop.handle.proc)
try
let pid = CoqTop.unixpid coqtop.handle.proc in
if Sys.os_type = "Win32" then begin
Copy link
Contributor

Choose a reason for hiding this comment

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

There is also a "Cygwin" value for this. Which windows setup does the issue occur on?

https://v2.ocaml.org/api/Sys.html

Copy link
Member Author

@jfehrle jfehrle Jun 7, 2022

Choose a reason for hiding this comment

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

The documentation you linked to says

"Cygwin" (for MS-Windows, OCaml compiled with Cygwin).

We don't support CoqIDE on Cygwin. Compiling CoqIDE with Cygwin libraries gives an unusable binary.

@jfehrle jfehrle force-pushed the fix_win_interrupt2 branch from a2b5c42 to d60e0da Compare June 7, 2022 20:51
@jfehrle
Copy link
Member Author

jfehrle commented Jun 7, 2022

Updated.

@MSoegtropIMC, would you be willing to test this? I don't know of any other developer who has Windows available.

@jfehrle
Copy link
Member Author

jfehrle commented Jun 7, 2022

@andrew-appel Your description of the behavior in #13550 is somewhat different from what I found. Please let me know if my description doesn't match the behavior you see.

If it doesn't match, what version of Windows do you use? Did you see the issue with the 32-bit binaries and/or 64-bit binaries? I'm using Windows 10 with 64-bit binaries.

@@ -0,0 +1,16 @@
#define _WIN32_WINNT 0x0501 /* Cf below, we restrict to */

#include <caml/mlvalues.h>
Copy link
Member

Choose a reason for hiding this comment

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

We already have a C stub file per OS, is there a solid reason to add a new one? I think it's not very good practice from the OCaml world to have a lot of C stubs lying around (especially given that they're preprocessed).

Copy link
Member Author

@jfehrle jfehrle Jun 8, 2022

Choose a reason for hiding this comment

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

I didn't want to add the second one. win32_cvtpid is used both in CoqIDE and in coqidetop. In the dune file, coqide_WIN32.ml.in is part of library coqide_gui. When I included win32_cvtpid in coqide_WIN32.c.in and included the library in the idetop executable as @rgrinberg suggested, I got very strange behavior in CoqIDE (delay of a second or so for each character entered). I'm guessing that all the files in the library were not merely linked, but in fact loaded. So their let () = ... expressions were executed rather than just the ones that were referenced, e.g. as in coqide_main.ml, which is bad.

If there's a way to link specific modules without loading everything in the library, I'm all for it. I didn't understand Rudi's suggestion here well enough to try it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I figured out a way to avoid the need for shared_WIN32.c.in and its friends. Now there's os_specific.ml which has the references to C code (usable in both coqide and coqidetop) and gui_specific.ml, containing items that depend on GTK and the GUI. Tedious to sort these out. If you're OK with this, I'll squash before the merge.

Copy link
Member Author

@jfehrle jfehrle Jun 10, 2022

Choose a reason for hiding this comment

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

I spoke too soon. It appears that Glib.Io.channel_of_descr_socket in GTK is Windows-only. Whatever library contains that can't be linked into coqtopide, while win32_cvtpid must be linked into both coqide and coqtopide. It appears they need to be two separate libraries. Let's go with the first commit. I'll remove the second one momentarily.

@jfehrle jfehrle force-pushed the fix_win_interrupt2 branch 2 times, most recently from 4fa8ec1 to 45ecf17 Compare June 10, 2022 20:55
@SkySkimmer SkySkimmer requested a review from a team August 30, 2022 11:44
@Alizter
Copy link
Contributor

Alizter commented Sep 5, 2022

While I've tested this a lot, it would be nice to get someone with a fresh perspective to try it on Windows in case I missed something.

This is something I believe most of the dev team is lacking unfortunately.

I've had a visual look through your changes and they seem sound to me. If you have been able to get interrupt working on Windows again, then I trust it has been done properly.

@MSoegtropIMC How do you test the Windows installations for Coq platform, especially CoqIDE. Would you be able to lend a hand testing this?

@MSoegtropIMC
Copy link
Contributor

@Alizter : I download the installer from CI, install it to some test folder and go through some test files from the smoke test kit in CoqIDE. I don't do fancy testing of CoqIDE - I would consider this to be part of Coq testing (not Coq Platform). In Coq Platform testing I concentrate on correct installation.

I am fairly busy with Coq Platform (and other things), but if there is no one else with a Windows machine, I could give it a try.

@Alizter
Copy link
Contributor

Alizter commented Sep 5, 2022

Thanks @MSoegtropIMC, whenever you have some spare time, if you could give this PR a test that would be most welcome.

@jfehrle
Copy link
Member Author

jfehrle commented Sep 5, 2022

if there is no one else with a Windows machine, I could give it a try.

Hmm, if a lack of hardware is the main problem for potential testers, that seems like a readily solvable problem. I suppose it's also possible to install Windows in a virtual machine, but that may require more work to set up/maintain. There's also a question of how often we need to test on Windows (and perhaps whether the other Coq IDEs have much need to test on Windows). I hope to make more improvements to CoqIDE fairly soon, though most of them shouldn't have OS-specific code (as does this one).

I think the harder part may be finding someone willing to test things from time to time.

@MSoegtropIMC
Copy link
Contributor

For installation in a VM you need a WIndows license and 16GB RAM.

Since I switched to Mac (and VSCoq for that matter - except when I need your Ltac debugger, which is frequent wnough) I am only doing essential testing for CoqIDE on Windows - which is not sufficient to guarantee the quality we want.

I will ask the Windows users I know if they can take some responsibility in testing Coq / CoqIDE / Coq Platform on Windows.

@github-actions github-actions bot added the needs: rebase Should be rebased on the latest master to solve conflicts or have a newer CI run. label Sep 20, 2022
@jfehrle jfehrle force-pushed the fix_win_interrupt2 branch from 45ecf17 to 5b06aa0 Compare October 6, 2022 05:53
@coqbot-app coqbot-app bot removed the needs: rebase Should be rebased on the latest master to solve conflicts or have a newer CI run. label Oct 6, 2022
@jfehrle
Copy link
Member Author

jfehrle commented Oct 6, 2022

Let's not delay this any more waiting for a tester. While it would be good to have a second tester for Windows specific changes, the relevant functionality is almost completely broken on Windows, so there is little risk it will be worse than before. I have no specific reason to expect this PR may not work for others.

@ppedrot If you have no further review comments, would you merge? Thanks.

@MSoegtropIMC
Copy link
Contributor

How about I take the patch into the 2022.09 beta release (I likely build the installers today and they should be signed on Monday)? There is anyway already a patch in for CoqIDE for the Mac preferences dialog crash.

@MSoegtropIMC
Copy link
Contributor

And I will send a reminder to the Windows users I had in mind ...

@MSoegtropIMC
Copy link
Contributor

I added this as opam level patch to the Coq Platform beta.

MSoegtropIMC added a commit to MSoegtropIMC/platform that referenced this pull request Oct 7, 2022
@jfehrle
Copy link
Member Author

jfehrle commented Oct 7, 2022

@MSoegtropIMC Sounds good, thanks.

@jfehrle
Copy link
Member Author

jfehrle commented Oct 7, 2022

Is this correct? The patch copied the code from the PR rather than depending on it, so if I change the code here, it won't affect the platform?

@MSoegtropIMC
Copy link
Contributor

@jfehrle : yes, a Coq Platform release is carved in stone. Since you asked this to be merged I guessed that this is appropriate.

@Alizter Alizter requested a review from ppedrot October 7, 2022 17:03
@jfehrle
Copy link
Member Author

jfehrle commented Oct 7, 2022

Great! I was thinking about when you did the debugger preview and my subsequent tweaking of the PR meant you had to update the platform a bit, too.

@MSoegtropIMC
Copy link
Contributor

This was a dev pick - the coqide package in it linked to a branch.

@Zimmi48
Copy link
Member

Zimmi48 commented Oct 10, 2022

@MSoegtropIMC I'm surprised to discover that this patch is included in the upcoming 8.16 beta Platform release. If that's the case, doesn't it mean that the patch should eventually be in 8.16.1 instead of 8.17? (cc @ppedrot)

@ppedrot ppedrot self-assigned this Oct 10, 2022
@ppedrot
Copy link
Member

ppedrot commented Oct 10, 2022

We can indeed backport this, it looks like an important bugfix.

@ppedrot ppedrot modified the milestones: 8.17+rc1, 8.16.1 Oct 10, 2022
Copy link
Member

@ppedrot ppedrot left a comment

Choose a reason for hiding this comment

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

Seems a bit contrived to me, but if it works...

@ppedrot
Copy link
Member

ppedrot commented Oct 13, 2022

@coqbot merge now

@coqbot-app coqbot-app bot merged commit 18145d8 into rocq-prover:master Oct 13, 2022
ppedrot added a commit to ppedrot/coq that referenced this pull request Oct 14, 2022
(foreign_stubs
(language c)
(names coqide_os_stubs))
(libraries coqide-server.protocol coqide-server.core lablgtk3-sourceview3))
(optional)
Copy link
Member

Choose a reason for hiding this comment

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

I think this optional wasn't meant to be added back

Copy link
Member

Choose a reason for hiding this comment

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

Not the first time I see some old stuff being added back. Be careful with how you resolve merge conflicts @jfehrle 😉

Copy link
Member Author

Choose a reason for hiding this comment

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

See #16672.

I am careful, but also fallible.

We could avoid some rebasing and resulting merge errors by reducing the long wait between writing the code and its merge (about 4 months for this fairly small PR). That's long enough to forget many details. It took me a couple hours over two days to remember how to build and retest the code on Windows. (The weirdest detail being that I can't push from the cygwin/Windows command line because I couldn't figure out how to set up the security token in Windows--as it was last time. I can only push from the WSL command line.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: fix This fixes a bug or incorrect documentation. part: CoqIDE Issues and PRs related to CoqIDE or other IDE features of coq.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CoqIDE interrupt doesn't work on Windows
6 participants