Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"symfony run" interactivity broken after v5.7.5 #404

Open
sarim opened this issue Jan 4, 2024 · 5 comments · Fixed by #405
Open

"symfony run" interactivity broken after v5.7.5 #404

sarim opened this issue Jan 4, 2024 · 5 comments · Fixed by #405

Comments

@sarim
Copy link
Contributor

sarim commented Jan 4, 2024

"symfony run" interactivity broken after ff1958f

In this commit runner code was changed, the specific line is

func buildCmd(cmd *exec.Cmd) error {
cmd.SysProcAttr = &syscall.SysProcAttr{
// isolate each command in a new process group that we can cleanly send
// signal to when we want to stop it
Setpgid: true,
}

Setting Setpgid to true loses stdin connection to terminal from the command, resulting in this.

$ symfony run psql
psql (15.5 (Ubuntu 15.5-1.pgdg22.04+1))
Type "help" for help.

It just hangs there. Anything typed in the terminal doesn't go through to the psql process. You can check by simple symfony run cat.

If I set Setpgid to false and compile it works. But that would obviously break the the original issue the referenced commit fixed. I think a proper solution would be tcgetpgrp, which we can make golang call if we also set Foreground: true

	cmd.SysProcAttr = &syscall.SysProcAttr{
		// isolate each command in a new process group that we can cleanly send
		// signal to when we want to stop it
		Setpgid:    true,
		Foreground: true,
	}

This fixes interactive commands. We have to take care to set Foreground: true only when user is running symfony run something interactively, not for worker/daemons. So Runner.Mode needs to be passed to buildCmd function.

I'll send a PR with complete fix.

update; PR opened #405

sarim added a commit to sarim/symfony-cli that referenced this issue Jan 4, 2024
sarim added a commit to sarim/symfony-cli that referenced this issue Jan 4, 2024
sarim added a commit to sarim/symfony-cli that referenced this issue Jan 4, 2024
fabpot added a commit that referenced this issue Jan 5, 2024
Set SysProcAttr Foreground: true when user run cmd. Fixes: #404
@sarim
Copy link
Contributor Author

sarim commented Jan 31, 2024

          Reverted fornow (with #410 as well) as it doesn't work (at least on Macos where it hangs and ^C doesn't work anymore).

Originally posted by @fabpot in #405 (comment)

@fabpot can we open this again as the fix was reverted? Can you provide a bit more info about how it doesn't work? From my testing I managed to reproduce the hangs and ^C doesn't work only if --watch flag was also provided. So a command like

symfony run --watch=config sleep 3600

It ONLY breaks after a change is detected, which kinda makes sense because we were making the cmd process to have its own pgid, then we are killing it from watcher goroutine from symfony. My strong suspicion is these leads to inconsistent behavior because of terminal's foreground behavior and read / write / piping std{in,our,err} of parent process and child process.

Can you confirm that it breaks for you for this use case ONLY? Other ways to invoke runner like

  • as daemon: symfony run -d ...... or from .symfony.local.yaml
  • interactively run but not having any watcher: symfony run sleep 3600
    Does not break it.

I can reproduce the hang even with currently published v5.8.6 (which doesn't have my SysProcAttr { Foreground: true } patch.

  1. /usr/bin/symfony -V: Symfony CLI version 5.8.6 (c) 2021-2024 Fabien Potencier (2024-01-30T13:12:32Z - stable)
  2. /usr/bin/symfony run --watch=config psql Interactivity broken, but possible to quit using ^C
  3. Run /usr/bin/symfony run --watch=config psql in one terminal, then echo >> config/routes.yaml in another terminal.
  4. Back to first terminal observe ^C doesn't work anymore.

If that's the case (broken only with watcher), I can think of two alternate solutions:

  1. Research about taking foreground control back after killing cmd pid from watcher notifier goroutine. This way might be tricky, but probably the technically correct solution.
  2. Bypass setting SysProcAttr altogether when run as interactive command. isInteractive will be derived from terminal.Stdin.IsInteractive, Runner.RunMode AND len (watchers). So if any watcher is provided it won't be regarded as interactive anymore. From my understanding we don't need to set SysProcAttr { Setpgid } when command is running interactively as terminal itself will send SIGINT to all process, so symfony doesn't need to manage and kill process tree in this mode. I've pushed a solution using this to my runner-interactivity branch.

Please advice :)

I'm actually very eager to restore the interactivity, as I regularly use the symfony run psql command. It's also taught in 'Symfony: The Fast Track' book.

@fabpot fabpot reopened this Jan 31, 2024
@carlos-granados
Copy link

I can conform this is happening to me as well, with version 5.8.15 under MacOS

@carlos-granados
Copy link

How can we connect to psql in the remote server without using symfony run psql given that this does not work?

@fabpot
Copy link
Contributor

fabpot commented May 9, 2024

#475 is my attempt to fix the problem. I did a few tests on MacOS and it works well. I need to test on Linux and Windows as well. @sarim Can you have a look?

@sarim
Copy link
Contributor Author

sarim commented May 9, 2024

@fabpot looks good in Ubuntu. I problem I described about hanging when run with --watch doesn't happen on your fix.


How can we connect to psql in the remote server without using symfony run psql given that this does not work?

@carlos-granados symfony var:export --multiline. Then manually copy paste and use the database connection arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants