Skip to content

Commit c9d0d25

Browse files
committed
cmd/buildlet/stage0: close serial port before running buildlet
Jeff thinks the stage0 binary is hogging the COM1 port and preventing the buildlet from using it. Updates golang/go#21153 Change-Id: I73e39eeed90269c179818d06864ab1c35ce9fa79 Reviewed-on: https://go-review.googlesource.com/51190 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 03d451a commit c9d0d25

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

cmd/buildlet/stage0/stage0.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ var (
4141
untarDestDir = flag.String("untar-dest-dir", "", "destination directory to untar --untar-file to")
4242
)
4343

44-
// configureSerialLogOutput is set non-nil on some platforms to configure
45-
// log output to go to the serial console.
46-
var configureSerialLogOutput func()
44+
// configureSerialLogOutput and closeSerialLogOutput are set non-nil
45+
// on some platforms to configure log output to go to the serial
46+
// console and to close the serial port, respectively.
47+
var (
48+
configureSerialLogOutput func()
49+
closeSerialLogOutput func()
50+
)
4751

4852
func main() {
4953
if configureSerialLogOutput != nil {
@@ -148,7 +152,16 @@ func main() {
148152
case "solaris/amd64":
149153
cmd.Args = append(cmd.Args, legacyReverseBuildletArgs("solaris-amd64-smartosbuildlet")...)
150154
}
155+
// Release the serial port (if we opened it) so the buildlet
156+
// process can open & write to it. At least on Windows, only
157+
// one process can have it open.
158+
if closeSerialLogOutput != nil {
159+
closeSerialLogOutput()
160+
}
151161
if err := cmd.Run(); err != nil {
162+
if configureSerialLogOutput != nil {
163+
configureSerialLogOutput()
164+
}
152165
sleepFatalf("Error running buildlet: %v", err)
153166
}
154167
}

cmd/buildlet/stage0/stage0_windows.go

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,22 @@ package main
66

77
import (
88
"log"
9+
"os"
910

1011
"github.com/tarm/serial"
1112
)
1213

1314
func init() {
1415
configureSerialLogOutput = configureSerialLogOutputWindows
16+
closeSerialLogOutput = closeSerialLogOutputWindows
1517
}
1618

19+
var com1 *serial.Port
20+
1721
func configureSerialLogOutputWindows() {
1822
c := &serial.Config{Name: "COM1", Baud: 9600}
19-
s, err := serial.OpenPort(c)
23+
var err error
24+
com1, err = serial.OpenPort(c)
2025
if err != nil {
2126
// Oh well, we tried. This empirically works
2227
// on Windows on GCE.
@@ -25,5 +30,13 @@ func configureSerialLogOutputWindows() {
2530
log.Printf("serial.OpenPort: %v", err)
2631
return
2732
}
28-
log.SetOutput(s)
33+
log.SetOutput(com1)
34+
}
35+
36+
func closeSerialLogOutputWindows() {
37+
if com1 != nil {
38+
com1.Close()
39+
com1 = nil
40+
log.SetOutput(os.Stderr)
41+
}
2942
}

0 commit comments

Comments
 (0)