Thread overview
[Issue 3462] New: Add a clean way to exit a process.
Nov 01, 2009
Leandro Lucarella
Nov 01, 2009
Leandro Lucarella
Nov 01, 2009
Leandro Lucarella
Nov 01, 2009
Sean Kelly
Nov 01, 2009
Leandro Lucarella
Jul 20, 2010
Sean Kelly
Jul 21, 2010
Leandro Lucarella
November 01, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3462

           Summary: Add a clean way to exit a process.
           Product: D
           Version: 2.035
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: patch
          Severity: enhancement
          Priority: P2
         Component: druntime
        AssignedTo: sean@invisibleduck.org
        ReportedBy: llucax@gmail.com


--- Comment #0 from Leandro Lucarella <llucax@gmail.com> 2009-10-31 21:46:07 PDT ---
Created an attachment (id=485)
druntime patch (against svn r185)

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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 01, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3462



--- Comment #1 from Leandro Lucarella <llucax@gmail.com> 2009-10-31 22:18:41 PDT ---
Created an attachment (id=486)
phobos patch (against svn r1316)

BTW, you can apply the patches with:

path/to/repo/trunk$ patch -p1 < patch

in the root directory of the svn repo.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 01, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3462



--- Comment #2 from Leandro Lucarella <llucax@gmail.com> 2009-10-31 22:26:43 PDT ---
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
}
--------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 01, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3462


Sean Kelly <sean@invisibleduck.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED


--- Comment #3 from Sean Kelly <sean@invisibleduck.org> 2009-10-31 23:15:05 PDT ---
This is tricky.  If multiple threads are running then the app has to either forcibly terminate the threads or wait for them to complete.  Even trickier if the ProcessExit exception isn't thrown from the main thread.  One could send a signal to all executing threads, telling them to throw an exception except that it isn't legal to throw an exception from a signal handler.

Sadly, I haven't come up with a way to do this that doesn't risk deadlocks or other Bad Things from happening.  As far as I know, the easiest thing is still to call cstdlib exit().  If a clean, safe option presents itself I'd gladly add a Runtime.exit() routine.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 01, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3462



--- Comment #4 from Leandro Lucarella <llucax@gmail.com> 2009-11-01 10:37:39 PST ---
Here is the NG discussion:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=99876

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 20, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3462



--- Comment #5 from Sean Kelly <sean@invisibleduck.org> 2010-07-20 15:03:10 PDT ---
Do you think this may have been addressed by the new messaging protocol in Phobos?  I know it's not druntime-specific, but the thread ownership hierarchy seems like a potentially decent solution to this problem.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 21, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=3462



--- Comment #6 from Leandro Lucarella <llucax@gmail.com> 2010-07-20 17:33:30 PDT ---
I'm not very familiar with the new threading model in D2, I'm mostly a D1 user, so I can't really say.

Sorry and thanks to take the time to answer.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------