Commit 916fead
Eric Bower
·
2025-12-25 23:45:05 -0500 EST
parent 167a764
fix(pipe): use ticker timer instead of sleep The time.Sleep in the default case blocks for 5 seconds, during which context cancellation is ignored. If the session ends, the goroutine won't notice until the sleep completes.
2 files changed,
+21,
-20
+20,
-19
| ... | ... | @@ -52,25 +52,6 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware { | |
| 52 | 52 | ||
| 53 | 53 | pipeCtx, cancel := context.WithCancel(ctx) | |
| 54 | 54 | ||
| 55 | - | go func() { | |
| 56 | - | defer cancel() | |
| 57 | - | ||
| 58 | - | for { | |
| 59 | - | select { | |
| 60 | - | case <-pipeCtx.Done(): | |
| 61 | - | return | |
| 62 | - | default: | |
| 63 | - | _, err := sesh.SendRequest("ping@pico.sh", false, nil) | |
| 64 | - | if err != nil { | |
| 65 | - | logger.Error("error sending ping", "err", err) | |
| 66 | - | return | |
| 67 | - | } | |
| 68 | - | ||
| 69 | - | time.Sleep(5 * time.Second) | |
| 70 | - | } | |
| 71 | - | } | |
| 72 | - | }() | |
| 73 | - | ||
| 74 | 55 | cliCmd := &CliCmd{ | |
| 75 | 56 | sesh: sesh, | |
| 76 | 57 | args: args, |
| ... | ... | @@ -118,6 +99,26 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware { | |
| 118 | 99 | sesh.RemoteAddr().String(), | |
| 119 | 100 | ) | |
| 120 | 101 | ||
| 102 | + | go func() { | |
| 103 | + | defer cancel() | |
| 104 | + | ||
| 105 | + | ticker := time.NewTicker(5 * time.Second) | |
| 106 | + | defer ticker.Stop() | |
| 107 | + | ||
| 108 | + | for { | |
| 109 | + | select { | |
| 110 | + | case <-pipeCtx.Done(): | |
| 111 | + | return | |
| 112 | + | case <-ticker.C: | |
| 113 | + | _, err := sesh.SendRequest("ping@pico.sh", false, nil) | |
| 114 | + | if err != nil { | |
| 115 | + | logger.Error("error sending ping", "err", err) | |
| 116 | + | return | |
| 117 | + | } | |
| 118 | + | } | |
| 119 | + | } | |
| 120 | + | }() | |
| 121 | + | ||
| 121 | 122 | switch cmd { | |
| 122 | 123 | case "pub": | |
| 123 | 124 | err := handler.pub(cliCmd, topic, clientID) |
+1,
-1
| ... | ... | @@ -144,7 +144,7 @@ func NewTestSSHServer(t *testing.T) *TestSSHServer { | |
| 144 | 144 | "pipe-ssh-test", | |
| 145 | 145 | "localhost", | |
| 146 | 146 | cfg.Port, | |
| 147 | - | "9222", | |
| 147 | + | "9223", | |
| 148 | 148 | "../../ssh_data/term_info_ed25519", | |
| 149 | 149 | func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { | |
| 150 | 150 | perms, _ := sshAuth.PubkeyAuthHandler(conn, key) |