mirror of
https://github.com/D-Modem/bbs.git
synced 2026-11-28 17:38:04 -08:00
Fix two races behind the flaky door test
TestDoorSession failed about once in 100-200 runs, two ways:
- "fork/exec door.sh: permission denied": the server made unix sockets
under a umask of 0177, and the umask is the whole process's. A file
made by another goroutine meanwhile lost its mode: here the test's
door script, written as the server started, came out 0600. In the
BBS the same could befall anything made while a listener started.
The socket is now made in a private directory beside its place, given
its mode and group there, then renamed into place: still no moment
when others could connect, and no umask touched. Closing the
listener removes the socket.
- the door's last output lost ("Bye from the door" never came): a door
without a pty wrote to cmd.StdoutPipe, which Wait closes as soon as
the door exits, while the reader may not have drained it. The door
now writes to a pipe of the runner's own, read to its end.
TestOutputAfterExit (door) lost 28-48 KB of a door's last output on
every run before; TestSocketLeavesUmask (server) saw 0755 files made
0600 within a few dozen tries. Both pass now, and TestDoorSession
passed 500 runs in a row under -race.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AFQhReUqUc7F5srk2hViqr
This commit is contained in:
4 files changed
+134
-14
No files matched your search
@@ -226,11 +226,18 @@ func (d *Door) Run(line Line, in *Info, utf8Term bool, idle time.Duration, logf
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
r, err := cmd.StdoutPipe()
|
||||
// a pipe of our own, not StdoutPipe's: Wait closes that when the
|
||||
// door exits, and what the door wrote last but was not yet read
|
||||
// would be lost. This one is read to its end (every writer gone).
|
||||
r, pw, err := os.Pipe()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
if err := cmd.Start(); err != nil {
|
||||
defer r.Close()
|
||||
cmd.Stdout = pw
|
||||
err = cmd.Start()
|
||||
pw.Close()
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
toDoor, fromDoor = w, r
|
||||
|
||||
@@ -183,3 +183,21 @@ func TestPTY(t *testing.T) {
|
||||
t.Errorf("output post-processed: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
// All a door writes reaches the caller, though it exits at once and the
|
||||
// caller is slow to read: the end of its output was lost when Wait closed
|
||||
// the pipe under the reader.
|
||||
func TestOutputAfterExit(t *testing.T) {
|
||||
d := script(t, `i=0
|
||||
while [ $i -lt 200 ]; do printf '%0999d\n' $i; i=$((i+1)); done
|
||||
printf 'END\r\n'
|
||||
`)
|
||||
d.Charset = ""
|
||||
c, done := caller(t, d, false, 0)
|
||||
time.Sleep(300 * time.Millisecond) // the door has exited; its last 64 KB wait in the pipe
|
||||
out, _ := io.ReadAll(c)
|
||||
if !strings.HasSuffix(string(out), "END\r\n") || len(out) != 200*1000+5 {
|
||||
t.Errorf("%d bytes, ending %q", len(out), out[max(0, len(out)-20):])
|
||||
}
|
||||
<-done
|
||||
}
|
||||
+37
-12
@@ -30,10 +30,10 @@ import (
|
||||
"net"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/racingmars/go3270"
|
||||
@@ -177,30 +177,55 @@ func (s *Server) listen(l *Listener) (net.Listener, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
// made private, then opened up to socket_mode: no moment in between
|
||||
// when others could connect
|
||||
old := syscall.Umask(0177)
|
||||
ln, err := net.Listen("unix", path)
|
||||
syscall.Umask(old)
|
||||
// made in a directory of its own that only this user can reach, given
|
||||
// its mode and group there, then moved into place: no moment when
|
||||
// others could connect, and no umask changed (the whole process's:
|
||||
// files others make meanwhile would lose their modes)
|
||||
tmp, err := os.MkdirTemp(filepath.Dir(path), ".s")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.Chmod(path, l.SocketMode); err != nil {
|
||||
ln.Close()
|
||||
defer os.RemoveAll(tmp)
|
||||
made := filepath.Join(tmp, "s")
|
||||
ln, err := net.Listen("unix", made)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ul := ln.(*net.UnixListener)
|
||||
ul.SetUnlinkOnClose(false) // the name it was made under is gone
|
||||
fail := func(err error) (net.Listener, error) {
|
||||
ul.Close()
|
||||
return nil, err
|
||||
}
|
||||
if err := os.Chmod(made, l.SocketMode); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
if l.SocketGroup != "" {
|
||||
g, err := user.LookupGroup(l.SocketGroup)
|
||||
if err == nil {
|
||||
gid, _ := strconv.Atoi(g.Gid)
|
||||
err = os.Chown(path, -1, gid)
|
||||
err = os.Chown(made, -1, gid)
|
||||
}
|
||||
if err != nil {
|
||||
ln.Close()
|
||||
return nil, fmt.Errorf("socket_group %s: %w", l.SocketGroup, err)
|
||||
return fail(fmt.Errorf("socket_group %s: %w", l.SocketGroup, err))
|
||||
}
|
||||
}
|
||||
return ln, nil
|
||||
if err := os.Rename(made, path); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
return &unixListener{ul, path}, nil
|
||||
}
|
||||
|
||||
// unixListener removes its socket when it is closed.
|
||||
type unixListener struct {
|
||||
*net.UnixListener
|
||||
path string
|
||||
}
|
||||
|
||||
func (u *unixListener) Close() error {
|
||||
err := u.UnixListener.Close()
|
||||
os.Remove(u.path)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Server) accept(ln net.Listener, l *Listener) {
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A unix socket is made with its mode from the start and without
|
||||
// touching the umask: files made meanwhile keep theirs (a door's script,
|
||||
// made as the server started, lost its x bits and would not run).
|
||||
func TestSocketLeavesUmask(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s := &Server{}
|
||||
stop := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; ; i++ {
|
||||
select {
|
||||
case <-stop:
|
||||
return
|
||||
default:
|
||||
}
|
||||
l := &Listener{Name: "s", Address: filepath.Join(dir, fmt.Sprintf("s%d.sock", i%4)), SocketMode: 0660}
|
||||
ln, err := s.listen(l)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
if fi, err := os.Stat(l.path()); err != nil || fi.Mode()&os.ModeSocket == 0 || fi.Mode().Perm() != 0660 {
|
||||
t.Errorf("socket: %v %v", fi.Mode(), err)
|
||||
}
|
||||
if c, err := net.Dial("unix", l.path()); err != nil {
|
||||
t.Errorf("dial: %v", err)
|
||||
} else {
|
||||
c.Close()
|
||||
}
|
||||
ln.Close()
|
||||
if _, err := os.Lstat(l.path()); !os.IsNotExist(err) {
|
||||
t.Errorf("socket left behind: %v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
want := os.FileMode(0755)
|
||||
probe := filepath.Join(t.TempDir(), "probe")
|
||||
os.WriteFile(probe, nil, 0755)
|
||||
if fi, err := os.Stat(probe); err == nil {
|
||||
want = fi.Mode().Perm() // as the umask leaves it
|
||||
}
|
||||
for i := 0; i < 3000; i++ {
|
||||
p := filepath.Join(dir, fmt.Sprintf("f%d", i))
|
||||
if err := os.WriteFile(p, []byte("#!/bin/sh\n"), 0755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if fi, _ := os.Stat(p); fi.Mode().Perm() != want {
|
||||
t.Fatalf("file %d made %v, want %v", i, fi.Mode().Perm(), want)
|
||||
}
|
||||
os.Remove(p)
|
||||
}
|
||||
close(stop)
|
||||
wg.Wait()
|
||||
if left, _ := filepath.Glob(filepath.Join(dir, ".s*")); len(left) > 0 {
|
||||
t.Errorf("left behind: %v", left)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user