Skip to content

Commit 5ac311e

Browse files
main: synchronize writes to VT100-faker on Windows
We use a third-party library "colorable" to translate VT100 color sequences into Windows console attribute-setting calls when Terraform is running on Windows. colorable is not concurrency-safe for multiple writes to the same console, because it writes to the console one character at a time and so two concurrent writers get their characters interleaved, creating unreadable garble. Here we wrap around it a synchronization mechanism to ensure that there can be only one Write call outstanding across both stderr and stdout, mimicking the usual behavior we expect (when stderr/stdout are a normal file handle) of each Write being completed atomically.
1 parent e76654a commit 5ac311e

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,15 @@ func copyOutput(r io.Reader, doneCh chan<- struct{}) {
258258
if runtime.GOOS == "windows" {
259259
stdout = colorable.NewColorableStdout()
260260
stderr = colorable.NewColorableStderr()
261+
262+
// colorable is not concurrency-safe when stdout and stderr are the
263+
// same console, so we need to add some synchronization to ensure that
264+
// we can't be concurrently writing to both stderr and stdout at
265+
// once, or else we get intermingled writes that create gibberish
266+
// in the console.
267+
wrapped := synchronizedWriters(stdout, stderr)
268+
stdout = wrapped[0]
269+
stderr = wrapped[1]
261270
}
262271

263272
var wg sync.WaitGroup

synchronized_writers.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"sync"
6+
)
7+
8+
type synchronizedWriter struct {
9+
io.Writer
10+
mutex *sync.Mutex
11+
}
12+
13+
// synchronizedWriters takes a set of writers and returns wrappers that ensure
14+
// that only one write can be outstanding at a time across the whole set.
15+
func synchronizedWriters(targets ...io.Writer) []io.Writer {
16+
mutex := &sync.Mutex{}
17+
ret := make([]io.Writer, len(targets))
18+
for i, target := range targets {
19+
ret[i] = &synchronizedWriter{
20+
Writer: target,
21+
mutex: mutex,
22+
}
23+
}
24+
return ret
25+
}
26+
27+
func (w *synchronizedWriter) Write(p []byte) (int, error) {
28+
w.mutex.Lock()
29+
defer w.mutex.Unlock()
30+
return w.Writer.Write(p)
31+
}

0 commit comments

Comments
 (0)