repos / pico

pico services mono repo
git clone https://github.com/picosh/pico.git

commit
916fead
parent
167a764
author
Eric Bower
date
2025-12-25 23:45:05 -0500 EST
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
M pkg/apps/pipe/cli.go
+20, -19
 1@@ -52,25 +52,6 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware {
 2 
 3 			pipeCtx, cancel := context.WithCancel(ctx)
 4 
 5-			go func() {
 6-				defer cancel()
 7-
 8-				for {
 9-					select {
10-					case <-pipeCtx.Done():
11-						return
12-					default:
13-						_, err := sesh.SendRequest("ping@pico.sh", false, nil)
14-						if err != nil {
15-							logger.Error("error sending ping", "err", err)
16-							return
17-						}
18-
19-						time.Sleep(5 * time.Second)
20-					}
21-				}
22-			}()
23-
24 			cliCmd := &CliCmd{
25 				sesh:     sesh,
26 				args:     args,
27@@ -118,6 +99,26 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware {
28 				sesh.RemoteAddr().String(),
29 			)
30 
31+			go func() {
32+				defer cancel()
33+
34+				ticker := time.NewTicker(5 * time.Second)
35+				defer ticker.Stop()
36+
37+				for {
38+					select {
39+					case <-pipeCtx.Done():
40+						return
41+					case <-ticker.C:
42+						_, err := sesh.SendRequest("ping@pico.sh", false, nil)
43+						if err != nil {
44+							logger.Error("error sending ping", "err", err)
45+							return
46+						}
47+					}
48+				}
49+			}()
50+
51 			switch cmd {
52 			case "pub":
53 				err := handler.pub(cliCmd, topic, clientID)
M pkg/apps/pipe/ssh_test.go
+1, -1
1@@ -144,7 +144,7 @@ func NewTestSSHServer(t *testing.T) *TestSSHServer {
2 		"pipe-ssh-test",
3 		"localhost",
4 		cfg.Port,
5-		"9222",
6+		"9223",
7 		"../../ssh_data/term_info_ed25519",
8 		func(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) {
9 			perms, _ := sshAuth.PubkeyAuthHandler(conn, key)