Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [Bug] Exiting shell from function called by trap handler always produces status 0
- X-seq: zsh-workers 43657
- From: Mikael Magnusson <mikachu@xxxxxxxxx>
- To: Peter Stephenson <p.stephenson@xxxxxxxxxxx>
- Subject: Re: [Bug] Exiting shell from function called by trap handler always produces status 0
- Date: Tue, 9 Oct 2018 11:58:05 +0200
- Cc: zsh-workers@xxxxxxx
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=anUhyhoBbUzOXi20XhoDRmpxXxmpOYTlGW17Horcxdo=; b=DEXwGhADIRdISeHj2aES+FHhGEizLcCJqQqqJqRhODche96SVYez4TDVKLMk3bwJNU slu1cRZCTv9q44R8/nrDl1+8bMlQ+caK6ZUjDfdfoOWt8o365ah1RkzlN5TaDsKec3YS ewYwEa1vCPr5tC8DVC1meYBeFH9Tdmr5QRB8CnYdzss1vJ5FHusENebeI2NKQS3fokyM 29zeIvIbtE5reqYN1W2UtEeyGJ+0nokHU4BfWK87bEB6wUjUqphOcq0ul1WtjYPzDnRi 7UPmbiW36HRxdTvFxGPi9LYq//nFxNMZMC2ptEXFyzu4YAGbrysjjvaTCHhHuHkZGlk2 RkDg==
- In-reply-to: <20181009084918eucas1p27dedda10d51beb773ba9175967912d2c~b5IUtvcVK3037630376eucas1p2R@eucas1p2.samsung.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- List-unsubscribe: <mailto:zsh-workers-unsubscribe@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <CGME20181009012624epcas1p44f2ae223f663713a980af4be735e5a3f@epcas1p4.samsung.com> <a5934536-7982-c434-d98d-2c07d66b0739@inlv.org> <20181009084918eucas1p27dedda10d51beb773ba9175967912d2c~b5IUtvcVK3037630376eucas1p2R@eucas1p2.samsung.com>
On 10/9/18, Peter Stephenson <p.stephenson@xxxxxxxxxxx> wrote:
> On Mon, 2018-10-08 at 14:02 +0100, Martijn Dekker wrote:
>> When a trap handler exits the shell using the 'exit' command within a
>> function, the shell's exit status is zero even if another exit status
>> was given as an argument to the 'exit' command.
>>
>> $ Src/zsh -c 'fn() { exit 13; }; trap "fn" EXIT'
>> $ echo $?
>> 0
>> (expected output: 13)
>
> diff --git a/Src/builtin.c b/Src/builtin.c
> index c5b319b..b81acdb 100644
> --- a/Src/builtin.c
> +++ b/Src/builtin.c
> @@ -5709,7 +5709,13 @@ int shell_exiting;
> mod_export void
> zexit(int val, int from_where)
> {
> - /* Don't do anything recursively: see below */
> + static int exit_val;
> + /*
> + * Don't do anything recursively: see below.
> + * Do, however, update exit status --- there's no nesting,
> + * a later value always overrides an earlier.
> + */
> + exit_val = val;
> if (shell_exiting == -1)
> return;
>
> @@ -5757,7 +5763,7 @@ zexit(int val, int from_where)
> #endif
> }
> }
> - lastval = val;
> + lastval = exit_val;
> /*
> * Now we are committed to exiting any previous state
> * is irrelevant. Ensure trap can run.
> @@ -5771,9 +5777,9 @@ zexit(int val, int from_where)
> release_pgrp();
> }
> if (mypid != getpid())
> - _exit(val);
> + _exit(exit_val);
> else
> - exit(val);
> + exit(exit_val);
> }
>
> /* . (dot), source */
> diff --git a/Test/C03traps.ztst b/Test/C03traps.ztst
> index dce263f..3b5d4c6 100644
> --- a/Test/C03traps.ztst
> +++ b/Test/C03traps.ztst
> @@ -863,6 +863,9 @@ F:Must be tested with a top-level script rather than
> source or function
> >a
> >b
>
> + $ZTST_testdir/../Src/zsh -fc 'fn() { exit 13; }; trap fn EXIT; exit'
> +13:Explicit exit in exit trap overrides status
> +
> %clean
>
> rm -f TRAPEXIT
We might as well make this test
+ $ZTST_testdir/../Src/zsh -fc 'fn() { exit $?+7; }; trap fn EXIT; exit 6'
+13:Explicit exit in exit trap overrides status
--
Mikael Magnusson
Messages sorted by:
Reverse Date,
Date,
Thread,
Author