cgsdev0 / bash-stack

modern web framework in bash
https://bashsta.cc
MIT License
525 stars 8 forks source link

note about command substitution #13

Closed dezren39 closed 8 months ago

dezren39 commented 8 months ago

if you run something like this

echo $(echo "value"; sleep 1 &)

there is no way to send sleep to background in bash. it will always wait.

this affects pages here, because pages are injected using $().

you can't send a response back, while also launching a process per-response that persists any length after the response.

i will solve this with a special file dir and a process launched ahead of time looking for files dropped off to launch, removing the long-running process from the command substitution. i am opening this because others may also encounter this. feel free to close. :-)

thanks for the fun project, very convenient except for all the bash.

dezren39 commented 8 months ago

image ok right now i'm launching background right before start.sh

./provider/background.sh &
./provider/start.sh

there's probably a better way

cgsdev0 commented 8 months ago

are you sure about this? i think i am doing something like this on https://connect4.bashsta.cc/

https://github.com/cgsdev0/bash-connect4/blob/4083f698cdba7738cf3efef3977061dfc74fbff5/pages/drop/%5Bteam%5D/%5Bcol%5D.sh#L148-L149

i remember this working fine, but i could totally be wrong

cgsdev0 commented 8 months ago

could you tell me more about what you're trying to do?

dezren39 commented 8 months ago

yeah i had something exactly like that, but it held the connection open indefinitely until the background process closed. the flow is something like

cgsdev0 commented 8 months ago

@dezren39 some questions

i'll try to see if i can repro

dezren39 commented 8 months ago

this is my laptop where i am hosting this, which is WSL. it could be WSL related, eventually it will be running in a vm somewhere and i can confirm if it reproduces elsewhere.

user@laptop-framework:~$ bash --version
bash --version
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

user@laptop-framework:~$ cat /etc/os-release
cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.4 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.4 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"

i just made this change locally and it now times out before sending the result

# cat << EOF > "$BACKGROUND_JOB_PATH"
# #!/usr/bin/env bash
# CHARM_DIR="$CHARM_DIR" $IDENTITY_DIR/identity charm link -d -o "$LINK_CODE_PATH" -k "${FORM_DATA[$key]}"
# EOF
CHARM_DIR="$CHARM_DIR" $IDENTITY_DIR/identity charm link -d -o "$LINK_CODE_PATH" -k "${FORM_DATA[$key]}" &

the binary doesn't read from stdin, it takes some args and then it outputs a file i look for and then eventually has an exit code based on if link was established.

i also tried using nohup/disown a little bit but i couldn't figure out a way to get them to work.

cgsdev0 commented 8 months ago

yeah ok, i am able to repro this too 😢

i will look into a solution

cgsdev0 commented 8 months ago

okay i think i'm starting to understand this

i think you need to redirect stdin, stdout, and stderr on the background process, otherwise it will block the exit

i created an example project: https://github.com/cgsdev0/bash-stack/tree/main/examples/bg-task

notable files:

i am using SSE here because it's currently the only mechanism for sending asynchronous data back to the user

dezren39 commented 8 months ago

ok i think i kind of understand, i can try and copy that example at least and see if it works for me, thank you

dezren39 commented 8 months ago

it works!!! thank you!