Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

The best defense against logic is ignorance.


devel / comp.unix.shell / Re: Orphaned subprocesses may cause command substitution to hang (was: When running xclip in a function, it cannot return to the main script.)

SubjectAuthor
* When running xclip in a function, it cannot return to the main script.hongy...@gmail.com
+* Re: When running xclip in a function, it cannot return to the main script.Helmut Waitzmann
|`* Re: When running xclip in a function, it cannot return to the main script.hongy...@gmail.com
| `* Orphaned subprocesses may cause command substitution to hang (was: When running Helmut Waitzmann
|  `* Re: Orphaned subprocesses may cause command substitution to hanghongy...@gmail.com
|   `- Re: Orphaned subprocesses may cause command substitution to hanghongy...@gmail.com
+- Re: When running xclip in a function, it cannot return to the mainChris Elvidge
`* Re: When running xclip in a function, it cannot return to the main script.Helmut Waitzmann
 `- Re: When running xclip in a function, it cannot return to the main script.hongy...@gmail.com

1
When running xclip in a function, it cannot return to the main script.

<4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5207&group=comp.unix.shell#5207

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:7dc8:0:b0:2e1:b3ec:6666 with SMTP id c8-20020ac87dc8000000b002e1b3ec6666mr2220168qte.556.1649153561332;
Tue, 05 Apr 2022 03:12:41 -0700 (PDT)
X-Received: by 2002:a05:620a:4402:b0:67d:1e5d:4899 with SMTP id
v2-20020a05620a440200b0067d1e5d4899mr1609298qkp.736.1649153561146; Tue, 05
Apr 2022 03:12:41 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Tue, 5 Apr 2022 03:12:40 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.156; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.156
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>
Subject: When running xclip in a function, it cannot return to the main script.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Tue, 05 Apr 2022 10:12:41 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 34
 by: hongy...@gmail.com - Tue, 5 Apr 2022 10:12 UTC

On Ubuntu 20.04.3 LTS, I check the self-compiled git master version of xclip with the following bash script:

```
#! /bin/bash
myclip() {
xclip -selection clipbord -in <<< "hello world"
echo x
return
} set -x
while true; do
y="$(myclip)"
done
```

It will be blocked there forever, as shown below:

```
$ bash myclip.sh
+ true
++ myclip
++ xclip -selection clipbord -in
++ echo x
++ return
^C
```

In other words, when running xclip in a function, it cannot return to the main script.

Please refer to the related discussion here [1].

[1] https://github.com/astrand/xclip/issues/134#issue-1192854776

Regards,
HZ

Re: When running xclip in a function, it cannot return to the main script.

<83h777tj5w.fsf@helmutwaitzmann.news.arcor.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5210&group=comp.unix.shell#5210

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!h0riuPVQZjwe5vAHiVUb8g.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: When running xclip in a function, it cannot return to the main script.
Date: Tue, 05 Apr 2022 21:38:35 +0200
Organization: Aioe.org NNTP Server
Message-ID: <83h777tj5w.fsf@helmutwaitzmann.news.arcor.de>
References: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="46900"; posting-host="h0riuPVQZjwe5vAHiVUb8g.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mail-Copies-To: nobody
Cancel-Lock: sha1:yDyzQrO4/Hu+kUhnxvDCtyykH0w=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Helmut Waitzmann - Tue, 5 Apr 2022 19:38 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
>On Ubuntu 20.04.3 LTS, I check the self-compiled git master version
>of xclip with the following bash script:
>
>```
>#! /bin/bash
>myclip() {
>xclip -selection clipbord -in <<< "hello world"
>echo x
>return
>}
>set -x
>while true; do
>y="$(myclip)"
>done
>```
>
>It will be blocked there forever, as shown below:
>
>
>```
>$ bash myclip.sh
>+ true
>++ myclip
>++ xclip -selection clipbord -in
>++ echo x
>++ return
>^C
>```
>
>In other words, when running xclip in a function, it cannot return
>to the main script.

Please verify your assumption by calling xclip directly without
using a shell function:

(
while sleep -- 1 && date -- '+%Y-%m-%dT%T%z'
do
y="$(
printf '%s\n' 'hello world' |
xclip -selection clipbord -in
)"
done
)

which – according to your statement – should print the current time
roughly once in a second, because there is no shell function
involved.  Does it do so?

>
>Please refer to the related discussion here [1].
>

(I prefer Usenet over WWW.)  The assumption mentioned in

>[1] https://github.com/astrand/xclip/issues/134#issue-1192854776

though, that the culprit is the forking xclip, is correct, but the
remedy "nohup" won't help.  Try

(
while sleep -- 1 && date -- '+%Y-%m-%dT%T%z'
do
y="$(
printf '%s\n' 'hello world' |
xclip -selection clipbord -in > /dev/null 2>&1
)"
done
)

which is the same as above except that the standard output and
standard error file descriptors of the "xclip" invocation are
redirected to "/dev/null".  Does it loop?  If it does, can you
explain, why?

Re: When running xclip in a function, it cannot return to the main script.

<t2iahs$m98$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5211&group=comp.unix.shell#5211

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: chr...@mshome.net (Chris Elvidge)
Newsgroups: comp.unix.shell
Subject: Re: When running xclip in a function, it cannot return to the main
script.
Date: Tue, 5 Apr 2022 21:58:20 +0100
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <t2iahs$m98$1@dont-email.me>
References: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 5 Apr 2022 20:58:36 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="eb83e8db2bdd4f943af55baf44f5904e";
logging-data="22824"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pjakOdQm7E5zSFIf4fqRFZx7XC0ePqbU="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101
Thunderbird/52.2.1 Lightning/5.4
Cancel-Lock: sha1:J5ucgaD1dMsvEFsQA7ty/8mWxnI=
In-Reply-To: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>
Content-Language: en-GB
 by: Chris Elvidge - Tue, 5 Apr 2022 20:58 UTC

On 05/04/2022 11:12, hongy...@gmail.com wrote:
> On Ubuntu 20.04.3 LTS, I check the self-compiled git master version of xclip with the following bash script:
>
> ```
> #! /bin/bash
> myclip() {
> xclip -selection clipbord -in <<< "hello world"
> echo x
> return
> }
> set -x
> while true; do
> y="$(myclip)"
> done
> ```
>
> It will be blocked there forever, as shown below:
>
> ```
> $ bash myclip.sh
> + true
> ++ myclip
> ++ xclip -selection clipbord -in
> ++ echo x
> ++ return
> ^C
> ```
>
> In other words, when running xclip in a function, it cannot return to the main script.
>
> Please refer to the related discussion here [1].
>
> [1] https://github.com/astrand/xclip/issues/134#issue-1192854776
>
> Regards,
> HZ
>

In a terminal:

myclip() {
xclip -selection clipbord -in <<< "hello world"
echo "X"
}

myclip note: it returns almost immediately
X

now do edit / paste

hello world

--
Chris Elvidge
England

Re: When running xclip in a function, it cannot return to the main script.

<83a6cztcr0.fsf@helmutwaitzmann.news.arcor.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5212&group=comp.unix.shell#5212

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!h0riuPVQZjwe5vAHiVUb8g.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Re: When running xclip in a function, it cannot return to the main script.
Date: Tue, 05 Apr 2022 23:57:07 +0200
Organization: Aioe.org NNTP Server
Message-ID: <83a6cztcr0.fsf@helmutwaitzmann.news.arcor.de>
References: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="30001"; posting-host="h0riuPVQZjwe5vAHiVUb8g.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Cancel-Lock: sha1:avKCLCH2NBEF3RbTXcw7qM/92cI=
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
X-Notice: Filtered by postfilter v. 0.9.2
 by: Helmut Waitzmann - Tue, 5 Apr 2022 21:57 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
>On Ubuntu 20.04.3 LTS, I check the self-compiled git master version
>of xclip with the following bash script:
>
>```
>#! /bin/bash
>myclip() {
>xclip -selection clipbord -in <<< "hello world"
>echo x
>return
>}
>set -x
>while true; do
>y="$(myclip)"
>done
>```
>
>It will be blocked there forever, as shown below:
>

[…]

>In other words, when running xclip in a function, it cannot return
>to the main script.

Modify the "myclip" function as shown here:

myclip()
{
printf '%s\n' 'hello world' |
xclip -selection clipboard -in > /dev/null
printf '%s\n' x
return
}

Then repeat your example from above with that modified "myclip"
function.  Does it block?

Re: When running xclip in a function, it cannot return to the main script.

<05af107d-6d6b-497f-83fa-4dddbbc5b163n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5214&group=comp.unix.shell#5214

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ae9:e8ce:0:b0:67b:1373:2e6c with SMTP id a197-20020ae9e8ce000000b0067b13732e6cmr4284399qkg.746.1649212545096;
Tue, 05 Apr 2022 19:35:45 -0700 (PDT)
X-Received: by 2002:a05:620a:4402:b0:67d:1e5d:4899 with SMTP id
v2-20020a05620a440200b0067d1e5d4899mr4310822qkp.736.1649212544855; Tue, 05
Apr 2022 19:35:44 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!1.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Tue, 5 Apr 2022 19:35:44 -0700 (PDT)
In-Reply-To: <83h777tj5w.fsf@helmutwaitzmann.news.arcor.de>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.156; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.156
References: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com> <83h777tj5w.fsf@helmutwaitzmann.news.arcor.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <05af107d-6d6b-497f-83fa-4dddbbc5b163n@googlegroups.com>
Subject: Re: When running xclip in a function, it cannot return to the main script.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Wed, 06 Apr 2022 02:35:45 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 115
 by: hongy...@gmail.com - Wed, 6 Apr 2022 02:35 UTC

On Wednesday, April 6, 2022 at 3:38:47 AM UTC+8, Helmut Waitzmann wrote:
> "hongy...@gmail.com" <hongy...@gmail.com>:
> >On Ubuntu 20.04.3 LTS, I check the self-compiled git master version
> >of xclip with the following bash script:
> >
> >```
> >#! /bin/bash
> >myclip() {
> >xclip -selection clipbord -in <<< "hello world"
> >echo x
> >return
> >}
> >set -x
> >while true; do
> >y="$(myclip)"
> >done
> >```
> >
> >It will be blocked there forever, as shown below:
> >
> >
> >```
> >$ bash myclip.sh
> >+ true
> >++ myclip
> >++ xclip -selection clipbord -in
> >++ echo x
> >++ return
> >^C
> >```
> >
> >In other words, when running xclip in a function, it cannot return
> >to the main script.
> Please verify your assumption by calling xclip directly without
> using a shell function:
>
> (
> while sleep -- 1 && date -- '+%Y-%m-%dT%T%z'
> do
> y="$(
> printf '%s\n' 'hello world' |
> xclip -selection clipbord -in
> )"
> done
> )
>
> which – according to your statement – should print the current time
> roughly once in a second, because there is no shell function
> involved. Does it do so?

No, it doesn't:

$ (
> while sleep -- 1 && date -- '+%Y-%m-%dT%T%z'
> do
> y="$(
> printf '%s\n' 'hello world' |
> xclip -selection clipbord -in
> )"
> done
> )
2022-04-06T10:31:24+0800
^C

> >
> >Please refer to the related discussion here [1].
> >
> (I prefer Usenet over WWW.) The assumption mentioned in
>
> >[1] https://github.com/astrand/xclip/issues/134#issue-1192854776
>
> though, that the culprit is the forking xclip, is correct, but the
> remedy "nohup" won't help. Try
>
> (
> while sleep -- 1 && date -- '+%Y-%m-%dT%T%z'
> do
> y="$(
> printf '%s\n' 'hello world' |
> xclip -selection clipbord -in > /dev/null 2>&1
> )"
> done
> )
>
> which is the same as above except that the standard output and
> standard error file descriptors of the "xclip" invocation are
> redirected to "/dev/null". Does it loop?

Yes. It does:

$ (
> while sleep -- 1 && date -- '+%Y-%m-%dT%T%z'
> do
> y="$(
> printf '%s\n' 'hello world' |
> xclip -selection clipbord -in > /dev/null 2>&1
> )"
> done
> )
2022-04-06T10:32:39+0800
2022-04-06T10:32:40+0800
2022-04-06T10:32:41+0800
2022-04-06T10:32:42+0800
2022-04-06T10:32:43+0800
2022-04-06T10:32:44+0800
2022-04-06T10:32:45+0800
2022-04-06T10:32:46+0800
^C

> If it does, can you explain, why?

I still don't understand

HZ

Re: When running xclip in a function, it cannot return to the main script.

<ccad471b-2ffd-4ced-b3a0-0fcc67a8f7d3n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5215&group=comp.unix.shell#5215

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:57d6:0:b0:2e0:68af:7c52 with SMTP id w22-20020ac857d6000000b002e068af7c52mr5690077qta.380.1649212939337;
Tue, 05 Apr 2022 19:42:19 -0700 (PDT)
X-Received: by 2002:ac8:5843:0:b0:2e1:f723:8e82 with SMTP id
h3-20020ac85843000000b002e1f7238e82mr5864840qth.3.1649212939191; Tue, 05 Apr
2022 19:42:19 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!nntp.club.cc.cmu.edu!45.76.7.193.MISMATCH!3.us.feeder.erje.net!feeder.erje.net!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Tue, 5 Apr 2022 19:42:19 -0700 (PDT)
In-Reply-To: <83a6cztcr0.fsf@helmutwaitzmann.news.arcor.de>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.156; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.156
References: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com> <83a6cztcr0.fsf@helmutwaitzmann.news.arcor.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ccad471b-2ffd-4ced-b3a0-0fcc67a8f7d3n@googlegroups.com>
Subject: Re: When running xclip in a function, it cannot return to the main script.
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Wed, 06 Apr 2022 02:42:19 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 79
 by: hongy...@gmail.com - Wed, 6 Apr 2022 02:42 UTC

On Wednesday, April 6, 2022 at 5:57:34 AM UTC+8, Helmut Waitzmann wrote:
> "hongy...@gmail.com" <hongy...@gmail.com>:
> >On Ubuntu 20.04.3 LTS, I check the self-compiled git master version
> >of xclip with the following bash script:
> >
> >```
> >#! /bin/bash
> >myclip() {
> >xclip -selection clipbord -in <<< "hello world"
> >echo x
> >return
> >}
> >set -x
> >while true; do
> >y="$(myclip)"
> >done
> >```
> >
> >It will be blocked there forever, as shown below:
> >
> […]
> >In other words, when running xclip in a function, it cannot return
> >to the main script.
> Modify the "myclip" function as shown here:
>
>
> myclip()
> {
> printf '%s\n' 'hello world' |
> xclip -selection clipboard -in > /dev/null
> printf '%s\n' x
> return
> }
>
> Then repeat your example from above with that modified "myclip"
> function. Does it block?

No, it works as expected:

$ cat myclip.sh
#! /bin/bash
myclip()
{ printf '%s\n' 'hello world' |
xclip -selection clipboard -in > /dev/null
printf '%s\n' x
sleep 1
return
}
set -x
while true; do
y="$(myclip)"
done

$ bash myclip.sh
+ true
++ myclip
++ printf '%s\n' 'hello world'
++ xclip -selection clipboard -in
++ printf '%s\n' x
++ sleep 1
++ return
+ y=x
+ true
++ myclip
++ printf '%s\n' 'hello world'
++ xclip -selection clipboard -in
++ printf '%s\n' x
++ sleep 1
++ return
+ y=x
+ true
++ myclip
++ printf '%s\n' 'hello world'
++ xclip -selection clipboard -in
^C

Regards,
HZ

Orphaned subprocesses may cause command substitution to hang (was: When running xclip in a function, it cannot return to the main script.)

<83czhurpc3.fsf_-_@helmutwaitzmann.news.arcor.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5220&group=comp.unix.shell#5220

 copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!aioe.org!QnwegDtla8bc2wLKNCWpYg.user.46.165.242.75.POSTED!not-for-mail
From: nn.throt...@xoxy.net (Helmut Waitzmann)
Newsgroups: comp.unix.shell
Subject: Orphaned subprocesses may cause command substitution to hang (was: When running xclip in a function, it cannot return to the main script.)
Date: Wed, 06 Apr 2022 21:20:28 +0200
Organization: Aioe.org NNTP Server
Message-ID: <83czhurpc3.fsf_-_@helmutwaitzmann.news.arcor.de>
References: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>
<83h777tj5w.fsf@helmutwaitzmann.news.arcor.de>
<05af107d-6d6b-497f-83fa-4dddbbc5b163n@googlegroups.com>
Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: quoted-printable
Injection-Info: gioia.aioe.org; logging-data="9543"; posting-host="QnwegDtla8bc2wLKNCWpYg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2 (gnu/linux)
Mail-Copies-To: nobody
Mail-Reply-To: Helmut Waitzmann Anti-Spam-Ticket.b.qc3c <oe.throttle@xoxy.net>
Cancel-Lock: sha1:l4+v1os3yOP/d04ee/2gDynhg2E=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Helmut Waitzmann - Wed, 6 Apr 2022 19:20 UTC

"hongy...@gmail.com" <hongyi.zhao@gmail.com>:
> On Wednesday, April 6, 2022 at 3:38:47 AM UTC+8, Helmut Waitzmann wrote:
>> "hongy...@gmail.com" <hongy...@gmail.com>:
>>> On Ubuntu 20.04.3 LTS, I check the self-compiled git master
>>> version of xclip with the following bash script:
>>>
>>> ```
>>> #! /bin/bash
>>> myclip() {
>>> xclip -selection clipbord -in <<< "hello world"
>>> echo x
>>> return
>>> }
>>> set -x
>>> while true; do
>>> y="$(myclip)"
>>> done
>>> ```
>>>
>>> It will be blocked there forever, as shown below:
>>>
>>>
>>> ```
>>> $ bash myclip.sh
>>> + true
>>> ++ myclip
>>> ++ xclip -selection clipbord -in
>>> ++ echo x
>>> ++ return
>>> ^C
>>> ```
>>>
>>> In other words, when running xclip in a function, it cannot
>>> return to the main script.
>
> I still don't understand
>

I'll try to explain:

The various variants of your example I gave in the messages with
the Message‐Ids <83h777tj5w.fsf@helmutwaitzmann.news.arcor.de> and
<83a6cztcr0.fsf@helmutwaitzmann.news.arcor.de> show:  It's not the
function that causes the blocking.  The script blocks if and only
if "xclip" is invoked without redirecting its standard output away
from feeding the command substitution "$( … )".  Whether that
occurs inside of a function or just inline makes no difference.

So, what's going on, there?

When the shell performs a command substition

"$( some commands )",

it first creates and opens a pipe by means of the system call
"pipe()". (A pipe is a kernel object which can be written into at
one end and read from at the other end.  See the manual pages
pipe(7) and pipe(2).)

Then the shell forks a process that will execute the subshell
environment: the command line inside the "$( )".

The forked subshell environment will write to the created pipe
while the outer shell, i. e. the shell that has forked the subshell
environment, will continue reading data from the created pipe and
collecting it until it reaches an end‐of‐file condition.  When it
has reached the end‐of‐file condition, it will wait by means of one
of the "wait" family of system calls (see the manual page wait(2))
for the subshell environment to terminate.

It puts the collected data it has read from the pipe into the
parameter list of the command.  In your example that command is the
variable assignment:

y="$( … )"

Therefore, the read data will get assigned to the shell variable
"y".

The exit status of the variable assignment will be the exit status
as obtained from the "wait" system call, i. e. it will be the exit
status of the command substitution.

The key to understanding why your example blocks is to understand
what an end‐of‐file condition in a pipe is:

When reading from a pipe, there are (at least) three conditions to
be distinguished:

(1) A writer has written something to the pipe.  Then upon reading,
the reader will get that data, which will be removed from the pipe.

(2) The pipe is empty but the writing end is still open for
writing, i.e. nothing has been written to it since all data that
may have been written to it have been read.  Then upon reading, the
reader will block and wait for further data to arrive as it does
not know whether the writer eventually will write further data.

(3) The writer may have written some (or may not have written any)
data to the pipe and then closes the writing end of the pipe.  When
there is no writer left to write to the writing end of the pipe,
then upon reading, the reader will get the data (if there is one)
like in (1) and then it will get an end‐of‐file condition.

The end‐of‐file condition tells the reader (in your use case: the
outer shell) that there will no further data arrive in the pipe (as
no one has it open for writing any more).

In the use case of the command substitution the outer shell will
know that it has read all data to be used in the command
substitution.  It will close its reading end of the pipe and then
wait for the subshell environment to terminate.

Now what's going on, when the subshell environment starts the
"xclip" command which will fork a subprocess?

Without redirection of its standard output, that is, the blocking
use cases, "xclip" will have its standard output file descriptor be
the writing end of the pipe.  When "xclip" forks its subprocess, the
subprocess will of course inherit the writing end of the pipe.  =>
There are two processes which might write to the pipe.

Now, if the "outer" "xclip" process terminates, its writing end of
the pipe will implicitly get closed, but the writing end of the pipe
hold by the forked "xclip" subprocess will remain open.

=> As written above in (2) and (3) the reader of the pipe won't get
an end‐of‐file condition as long as the forked "xclip" subprocess
neither closes its standard output (Of course it will not:  No
program is expected to close its standard output.) nor terminates.

=> Therefore the outer shell will wait for the subshell environment
to send an end‐of‐file condition to the pipe until the cows come
home.

Now on the other hand, if "xclip" is called with its standard
output file descriptor redirected from the writing end of the pipe
to for example "/dev/null", then its forked subprocess will inherit
the redirected standard output file descriptor.

=> No dangling writing end of the pipe will stay open and the outer
shell will finally get its longed‐for end‐of‐file condition when
reading from the pipe.

That scenario is not specific to "xclip".  Here is an example using
"sleep" in the background.  Note:  "sleep" won't write anything to
its standard output, and of course it won't even bother closing it:

(
while var="$(
sleep -- 2 &
date -- '+%Y-%m-%dT%T%z'
)"
do
printf 'command substitution ran at %s\n' "$var"
date -- '+command substitution ended at %Y-%m-%dT%T%z'
echo
sleep -- 1
done
)

The output shows that the command substitution lasts for 2 seconds
in spite of the "sleep" command being run in the background,
because the end‐of‐file condition for the reader is delayed until
that two‐seconds‐sleep terminates and implicitly gets it standard
output file descriptor, which refers to the writing end of the
pipe, closed.

If the standard output of the "sleep" command is redirected from
the writing end of the pipe to (for example) "/dev/null" then the
command substitution returns almost immediately, as can be seen
here:

(
while var="$(
sleep -- 2 > /dev/null &
date -- '+%Y-%m-%dT%T%z'
)"
do
printf 'command substitution ran at %s\n' "$var"
date -- '+command substitution ended at %Y-%m-%dT%T%z'
echo
sleep -- 1
done
)

May I add my refrain?  Knowing linux or unix greatly helps
understanding the shell.  Without that knowledge one will have a
hard job doing so.

Re: Orphaned subprocesses may cause command substitution to hang (was: When running xclip in a function, it cannot return to the main script.)

<7602ae6d-9dab-4a24-b866-072ac830714dn@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5222&group=comp.unix.shell#5222

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:6214:1d05:b0:444:729:8386 with SMTP id e5-20020a0562141d0500b0044407298386mr1531537qvd.77.1649294294418;
Wed, 06 Apr 2022 18:18:14 -0700 (PDT)
X-Received: by 2002:ac8:5b56:0:b0:2e2:391b:f107 with SMTP id
n22-20020ac85b56000000b002e2391bf107mr9987462qtw.275.1649294294257; Wed, 06
Apr 2022 18:18:14 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Wed, 6 Apr 2022 18:18:14 -0700 (PDT)
In-Reply-To: <83czhurpc3.fsf_-_@helmutwaitzmann.news.arcor.de>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.156; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.156
References: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>
<83h777tj5w.fsf@helmutwaitzmann.news.arcor.de> <05af107d-6d6b-497f-83fa-4dddbbc5b163n@googlegroups.com>
<83czhurpc3.fsf_-_@helmutwaitzmann.news.arcor.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <7602ae6d-9dab-4a24-b866-072ac830714dn@googlegroups.com>
Subject: Re: Orphaned subprocesses may cause command substitution to hang
(was: When running xclip in a function, it cannot return to the main script.)
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Thu, 07 Apr 2022 01:18:14 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 47
 by: hongy...@gmail.com - Thu, 7 Apr 2022 01:18 UTC

On Thursday, April 7, 2022 at 3:25:41 AM UTC+8, Helmut Waitzmann wrote:
> "hongy...@gmail.com" <hongy...@gmail.com>:
> > On Wednesday, April 6, 2022 at 3:38:47 AM UTC+8, Helmut Waitzmann wrote:
> >> "hongy...@gmail.com" <hongy...@gmail.com>:
> >>> On Ubuntu 20.04.3 LTS, I check the self-compiled git master
> >>> version of xclip with the following bash script:
> >>>
> >>> ```
> >>> #! /bin/bash
> >>> myclip() {
> >>> xclip -selection clipbord -in <<< "hello world"
> >>> echo x
> >>> return
> >>> }
> >>> set -x
> >>> while true; do
> >>> y="$(myclip)"
> >>> done
> >>> ```
> >>>
> >>> It will be blocked there forever, as shown below:
> >>>
> >>>
> >>> ```
> >>> $ bash myclip.sh
> >>> + true
> >>> ++ myclip
> >>> ++ xclip -selection clipbord -in
> >>> ++ echo x
> >>> ++ return
> >>> ^C
> >>> ```
> >>>
> >>> In other words, when running xclip in a function, it cannot
> >>> return to the main script.
> >
> > I still don't understand
> >
> I'll try to explain:
> [snipped]
> May I add my refrain? Knowing linux or unix greatly helps
> understanding the shell. Without that knowledge one will have a
> hard job doing so.

This is really not an easy problem to analyze without the deep understanding of the corresponding Linux or Unix underlying communication mechanism. Thank you for your in-depth and systematic analysis and nice examples.

Regards,
HZ

Re: Orphaned subprocesses may cause command substitution to hang (was: When running xclip in a function, it cannot return to the main script.)

<764b316d-01f0-4b19-b600-4588329bc090n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=5223&group=comp.unix.shell#5223

 copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:a05:6214:29c7:b0:444:196:c9c6 with SMTP id gh7-20020a05621429c700b004440196c9c6mr2638871qvb.70.1649295088226;
Wed, 06 Apr 2022 18:31:28 -0700 (PDT)
X-Received: by 2002:a05:622a:2d6:b0:2e2:1294:4a80 with SMTP id
a22-20020a05622a02d600b002e212944a80mr9929080qtx.256.1649295088045; Wed, 06
Apr 2022 18:31:28 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Wed, 6 Apr 2022 18:31:27 -0700 (PDT)
In-Reply-To: <7602ae6d-9dab-4a24-b866-072ac830714dn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.156; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.156
References: <4edca48a-eada-485d-bf99-4e55fd5cd1b3n@googlegroups.com>
<83h777tj5w.fsf@helmutwaitzmann.news.arcor.de> <05af107d-6d6b-497f-83fa-4dddbbc5b163n@googlegroups.com>
<83czhurpc3.fsf_-_@helmutwaitzmann.news.arcor.de> <7602ae6d-9dab-4a24-b866-072ac830714dn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <764b316d-01f0-4b19-b600-4588329bc090n@googlegroups.com>
Subject: Re: Orphaned subprocesses may cause command substitution to hang
(was: When running xclip in a function, it cannot return to the main script.)
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Thu, 07 Apr 2022 01:31:28 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 57
 by: hongy...@gmail.com - Thu, 7 Apr 2022 01:31 UTC

On Thursday, April 7, 2022 at 9:18:16 AM UTC+8, hongy...@gmail.com wrote:
> On Thursday, April 7, 2022 at 3:25:41 AM UTC+8, Helmut Waitzmann wrote:
> > "hongy...@gmail.com" <hongy...@gmail.com>:
> > > On Wednesday, April 6, 2022 at 3:38:47 AM UTC+8, Helmut Waitzmann wrote:
> > >> "hongy...@gmail.com" <hongy...@gmail.com>:
> > >>> On Ubuntu 20.04.3 LTS, I check the self-compiled git master
> > >>> version of xclip with the following bash script:
> > >>>
> > >>> ```
> > >>> #! /bin/bash
> > >>> myclip() {
> > >>> xclip -selection clipbord -in <<< "hello world"
> > >>> echo x
> > >>> return
> > >>> }
> > >>> set -x
> > >>> while true; do
> > >>> y="$(myclip)"
> > >>> done
> > >>> ```
> > >>>
> > >>> It will be blocked there forever, as shown below:
> > >>>
> > >>>
> > >>> ```
> > >>> $ bash myclip.sh
> > >>> + true
> > >>> ++ myclip
> > >>> ++ xclip -selection clipbord -in
> > >>> ++ echo x
> > >>> ++ return
> > >>> ^C
> > >>> ```
> > >>>
> > >>> In other words, when running xclip in a function, it cannot
> > >>> return to the main script.
> > >
> > > I still don't understand
> > >
> > I'll try to explain:
> > [snipped]
> > May I add my refrain? Knowing linux or unix greatly helps
> > understanding the shell. Without that knowledge one will have a
> > hard job doing so.
> This is really not an easy problem to analyze without the deep understanding of the corresponding Linux or Unix underlying communication mechanism. Thank you for your in-depth and systematic analysis and nice examples.

Dear Helmut Waitzmann,

I noticed the information of the following contributor [1]:

Helmut Waitzmann Helmut.Waitzmann@web.de

I don't know if this is you.

[1] https://github.com/landlock-lsm/tar/blob/master/THANKS#L209

Regards,
HZ

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor