digitalmars.D - Add a clean way to exit a process
- Leandro Lucarella (62/62) Oct 31 2009 Hi. I won't be very verbose about this because I submitted a bug about i...
- Sean Kelly (1/1) Nov 01 2009 I've thought about doing this before, but I haven't come up with a clean...
- Leandro Lucarella (22/31) Nov 01 2009 Yes, I completely ignored MT programs because in that case is usually
Hi. I won't be very verbose about this because I submitted a bug about it (with a patch): http://d.puremagic.com/issues/show_bug.cgi?id=3462 But I think it might deserve some public discussion too, that's why I'm bringing it here. For the lazy, here is the bug report text: Maybe I'm missing something, but I can't find any "standard" way to exit a program cleanly. I can't just call C's exit() function because the stack doesn't get unwinded so scope guards and other finally blocks are not executed. This is bad if you want to do some cleanup. In my particular case, I create a lock file so other instances of a program exit immediately if the lock file is present, and I want to remove the lock file as soon as the program finishes. I want to be able to call some exit() function in any part of the program for simplicity though. I hacked a solution by adding all the program inside a try block, catching an special "Exit" exception with a status attribute. If that exception is catched, the program returns the exception's status code. I think this is an useful feature that deserves being in the standard library, and for that we need runtime support (that's why I added it to the druntime component instead of Phobos, even when Phobos should be hacked too. Attached are patches for druntime and phobos with this changes: druntime: * Add a ProcessExit class to core.exception module (inherits from Object since it's not a real exception and we don't want people catching it even when doing a catch (Throwable)). * Catch the new exception in tryExec() nested funtion from dmain2.d, even when rt_trapExceptions is false (again, because is not really an exception), making the status attribute the new program's exit code. phobos: * Add a new void std.process.exit(int status). All it does is "throw new ProcessExit(status);" to hide implementation details. I don't know if std.process is the better module, maybe it should go somewhere else. If this is accepted, I guess the discussion in this thread should be revised: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=99432 Personally I think: -------------------- try { // code } catch { // code } -------------------- Should be a shortcut to: -------------------- try { // code } catch (Exception) { // code } -------------------- Thanks! -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ ---------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------- For me to ask a woman out, I've got to get into a mental state like the karate guys before they break the bricks. -- George Constanza
Oct 31 2009
I've thought about doing this before, but I haven't come up with a clean way to do it yet. The problem with simply throwing an exception is that this would only work in the main thread. To allow other threads to terminate the process you'd have to forcibly terminate other threads, and then running dtors would result in undefined behavior. I'd considered sending signals to all threads and having them all throw exceptions, but it's illegal to throw an exception from a signal hander, and Windows has poor signal support anyway. Sadly, I don't know of a better solution than simply calling exit() right now.
Nov 01 2009
Sean Kelly, el 1 de noviembre a las 12:23 me escribiste:I've thought about doing this before, but I haven't come up with a clean way to do it yet. The problem with simply throwing an exception is that this would only work in the main thread. To allow other threads toYes, I completely ignored MT programs because in that case is usually a little more tricky to stop everithing in a general way. When you have MT you're probably stucked with manual termination.terminate the process you'd have to forcibly terminate other threads, and then running dtors would result in undefined behavior. I'd considered sending signals to all threads and having them all throw exceptions, but it's illegal to throw an exception from a signal hander, and Windows has poor signal support anyway. Sadly, I don't know of a better solution than simply calling exit() right now.I guess something can be hacked using pthread_cancel + pthread_cleanup, but even if this would be possible I think it's too much complexity for too little gain (and again, I don't know if Windows as a similar mechanism). Anyway, I think the common case for a clean exit() are small script-ish programs (I came up with this problem porting a small Python script to D), not big multi-threaded applications. Maybe we can have a simple limited solution: just add this check to the exit() function in my patch: assert (!thread_needLock()); or something similar that can tell us if there are any other threads running. -- Leandro Lucarella (AKA luca) http://llucax.com.ar/ ---------------------------------------------------------------------- GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05) ---------------------------------------------------------------------- SATANAS EN COMISARIA -- Crónica TV
Nov 01 2009