Proper handling of two consecutive UserInterrupt in Haskell












0














My program is running a quite long iterative IO computation. At each iteration, I'm saving an intermediary result in case of a shutdown request. Upon such a request I want to stop the computation and print the intermediary result before terminating the program. Here is what I achieved, it works, but when I press CTRL-C more than once in my terminal, the final print is not done. This is the first time I have to deal with asynchronous exceptions, so I may be wrong.



main :: String -> IO ()
main query = uninterruptibleMask $ restore -> do
results <- newIORef
xe <- try $ restore $ search query results
case xe of
Right x -> printCompleted >> printResults x
Left e -> do
printInterrupted
readIORef results >>= printResults
case e of
UserInterrupt -> return ()
_ -> throw e


Thanks!










share|improve this question






















  • This seems like a pretty normal behavior for a program to have. The user hits ^C once: okay, fine, you want me to quit, give me a minute and I'll print what I know so far. The user hits ^C again while this is happening? They are tired of waiting for the program to quit, and want it to just get out of the way so they can run some other command.
    – amalloy
    Nov 12 '18 at 19:23










  • @amalloy sure, but since CTRL-C (always?) sends SIGINT, I would expect this piece of code to always work since it's using uninterruptibleMask.
    – Timothée Jourde
    Nov 12 '18 at 19:28










  • You are interruptible while printing, because you have called restore once query completes, no? I am not very familiar with async exceptions either, but from reading the Control.Exception docs it seems that the restore argument exists to allow you to restore the masking state so you can say you no longer need to be masked.
    – amalloy
    Nov 12 '18 at 19:41












  • @amalloy I'm only calling restore inside try and the final print (printInterrupted and printResults) is done outside try.
    – Timothée Jourde
    Nov 12 '18 at 19:46






  • 1




    Apparently, the Haskell runtime kills the program on the second Ctrl-C by default, unless a signal handler is installed stackoverflow.com/questions/2349233/…
    – danidiaz
    Nov 12 '18 at 20:27
















0














My program is running a quite long iterative IO computation. At each iteration, I'm saving an intermediary result in case of a shutdown request. Upon such a request I want to stop the computation and print the intermediary result before terminating the program. Here is what I achieved, it works, but when I press CTRL-C more than once in my terminal, the final print is not done. This is the first time I have to deal with asynchronous exceptions, so I may be wrong.



main :: String -> IO ()
main query = uninterruptibleMask $ restore -> do
results <- newIORef
xe <- try $ restore $ search query results
case xe of
Right x -> printCompleted >> printResults x
Left e -> do
printInterrupted
readIORef results >>= printResults
case e of
UserInterrupt -> return ()
_ -> throw e


Thanks!










share|improve this question






















  • This seems like a pretty normal behavior for a program to have. The user hits ^C once: okay, fine, you want me to quit, give me a minute and I'll print what I know so far. The user hits ^C again while this is happening? They are tired of waiting for the program to quit, and want it to just get out of the way so they can run some other command.
    – amalloy
    Nov 12 '18 at 19:23










  • @amalloy sure, but since CTRL-C (always?) sends SIGINT, I would expect this piece of code to always work since it's using uninterruptibleMask.
    – Timothée Jourde
    Nov 12 '18 at 19:28










  • You are interruptible while printing, because you have called restore once query completes, no? I am not very familiar with async exceptions either, but from reading the Control.Exception docs it seems that the restore argument exists to allow you to restore the masking state so you can say you no longer need to be masked.
    – amalloy
    Nov 12 '18 at 19:41












  • @amalloy I'm only calling restore inside try and the final print (printInterrupted and printResults) is done outside try.
    – Timothée Jourde
    Nov 12 '18 at 19:46






  • 1




    Apparently, the Haskell runtime kills the program on the second Ctrl-C by default, unless a signal handler is installed stackoverflow.com/questions/2349233/…
    – danidiaz
    Nov 12 '18 at 20:27














0












0








0







My program is running a quite long iterative IO computation. At each iteration, I'm saving an intermediary result in case of a shutdown request. Upon such a request I want to stop the computation and print the intermediary result before terminating the program. Here is what I achieved, it works, but when I press CTRL-C more than once in my terminal, the final print is not done. This is the first time I have to deal with asynchronous exceptions, so I may be wrong.



main :: String -> IO ()
main query = uninterruptibleMask $ restore -> do
results <- newIORef
xe <- try $ restore $ search query results
case xe of
Right x -> printCompleted >> printResults x
Left e -> do
printInterrupted
readIORef results >>= printResults
case e of
UserInterrupt -> return ()
_ -> throw e


Thanks!










share|improve this question













My program is running a quite long iterative IO computation. At each iteration, I'm saving an intermediary result in case of a shutdown request. Upon such a request I want to stop the computation and print the intermediary result before terminating the program. Here is what I achieved, it works, but when I press CTRL-C more than once in my terminal, the final print is not done. This is the first time I have to deal with asynchronous exceptions, so I may be wrong.



main :: String -> IO ()
main query = uninterruptibleMask $ restore -> do
results <- newIORef
xe <- try $ restore $ search query results
case xe of
Right x -> printCompleted >> printResults x
Left e -> do
printInterrupted
readIORef results >>= printResults
case e of
UserInterrupt -> return ()
_ -> throw e


Thanks!







haskell asynchronous exception






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 19:14









Timothée JourdeTimothée Jourde

518




518












  • This seems like a pretty normal behavior for a program to have. The user hits ^C once: okay, fine, you want me to quit, give me a minute and I'll print what I know so far. The user hits ^C again while this is happening? They are tired of waiting for the program to quit, and want it to just get out of the way so they can run some other command.
    – amalloy
    Nov 12 '18 at 19:23










  • @amalloy sure, but since CTRL-C (always?) sends SIGINT, I would expect this piece of code to always work since it's using uninterruptibleMask.
    – Timothée Jourde
    Nov 12 '18 at 19:28










  • You are interruptible while printing, because you have called restore once query completes, no? I am not very familiar with async exceptions either, but from reading the Control.Exception docs it seems that the restore argument exists to allow you to restore the masking state so you can say you no longer need to be masked.
    – amalloy
    Nov 12 '18 at 19:41












  • @amalloy I'm only calling restore inside try and the final print (printInterrupted and printResults) is done outside try.
    – Timothée Jourde
    Nov 12 '18 at 19:46






  • 1




    Apparently, the Haskell runtime kills the program on the second Ctrl-C by default, unless a signal handler is installed stackoverflow.com/questions/2349233/…
    – danidiaz
    Nov 12 '18 at 20:27


















  • This seems like a pretty normal behavior for a program to have. The user hits ^C once: okay, fine, you want me to quit, give me a minute and I'll print what I know so far. The user hits ^C again while this is happening? They are tired of waiting for the program to quit, and want it to just get out of the way so they can run some other command.
    – amalloy
    Nov 12 '18 at 19:23










  • @amalloy sure, but since CTRL-C (always?) sends SIGINT, I would expect this piece of code to always work since it's using uninterruptibleMask.
    – Timothée Jourde
    Nov 12 '18 at 19:28










  • You are interruptible while printing, because you have called restore once query completes, no? I am not very familiar with async exceptions either, but from reading the Control.Exception docs it seems that the restore argument exists to allow you to restore the masking state so you can say you no longer need to be masked.
    – amalloy
    Nov 12 '18 at 19:41












  • @amalloy I'm only calling restore inside try and the final print (printInterrupted and printResults) is done outside try.
    – Timothée Jourde
    Nov 12 '18 at 19:46






  • 1




    Apparently, the Haskell runtime kills the program on the second Ctrl-C by default, unless a signal handler is installed stackoverflow.com/questions/2349233/…
    – danidiaz
    Nov 12 '18 at 20:27
















This seems like a pretty normal behavior for a program to have. The user hits ^C once: okay, fine, you want me to quit, give me a minute and I'll print what I know so far. The user hits ^C again while this is happening? They are tired of waiting for the program to quit, and want it to just get out of the way so they can run some other command.
– amalloy
Nov 12 '18 at 19:23




This seems like a pretty normal behavior for a program to have. The user hits ^C once: okay, fine, you want me to quit, give me a minute and I'll print what I know so far. The user hits ^C again while this is happening? They are tired of waiting for the program to quit, and want it to just get out of the way so they can run some other command.
– amalloy
Nov 12 '18 at 19:23












@amalloy sure, but since CTRL-C (always?) sends SIGINT, I would expect this piece of code to always work since it's using uninterruptibleMask.
– Timothée Jourde
Nov 12 '18 at 19:28




@amalloy sure, but since CTRL-C (always?) sends SIGINT, I would expect this piece of code to always work since it's using uninterruptibleMask.
– Timothée Jourde
Nov 12 '18 at 19:28












You are interruptible while printing, because you have called restore once query completes, no? I am not very familiar with async exceptions either, but from reading the Control.Exception docs it seems that the restore argument exists to allow you to restore the masking state so you can say you no longer need to be masked.
– amalloy
Nov 12 '18 at 19:41






You are interruptible while printing, because you have called restore once query completes, no? I am not very familiar with async exceptions either, but from reading the Control.Exception docs it seems that the restore argument exists to allow you to restore the masking state so you can say you no longer need to be masked.
– amalloy
Nov 12 '18 at 19:41














@amalloy I'm only calling restore inside try and the final print (printInterrupted and printResults) is done outside try.
– Timothée Jourde
Nov 12 '18 at 19:46




@amalloy I'm only calling restore inside try and the final print (printInterrupted and printResults) is done outside try.
– Timothée Jourde
Nov 12 '18 at 19:46




1




1




Apparently, the Haskell runtime kills the program on the second Ctrl-C by default, unless a signal handler is installed stackoverflow.com/questions/2349233/…
– danidiaz
Nov 12 '18 at 20:27




Apparently, the Haskell runtime kills the program on the second Ctrl-C by default, unless a signal handler is installed stackoverflow.com/questions/2349233/…
– danidiaz
Nov 12 '18 at 20:27












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268659%2fproper-handling-of-two-consecutive-userinterrupt-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53268659%2fproper-handling-of-two-consecutive-userinterrupt-in-haskell%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma