Fix goroutine leak in the nullio reader

This commit is contained in:
Maximilian Paß
2021-12-11 22:04:20 +01:00
parent c565ca217e
commit 9f0b04660f
3 changed files with 30 additions and 8 deletions

View File

@@ -1,16 +1,31 @@
package nullio
import (
"context"
"fmt"
"io"
)
// Reader is a struct that implements the io.Reader interface. Read does not return when called.
type Reader struct{}
// Reader is a struct that implements the io.Reader interface.
// Read does not return when called until the context is done. It is used to avoid reading anything and returning io.EOF
// before the context has finished.
// For example the reader is used by the execution that fetches the stderr stream from Nomad. We do not have a stdin
// that we want to send to Nomad. But we still have to pass Nomad a reader.
// Normally readers send an io.EOF as soon as they have nothing more to read. But we want to avoid this, because in that
// case Nomad will abort (not the execution but) the transmission.
// Why should the reader not just always return 0, nil? Because Nomad reads in an endless loop and thus a busy waiting
// is avoided.
type Reader struct {
Ctx context.Context
}
func (r Reader) Read(_ []byte) (int, error) {
// An empty select blocks forever.
select {}
if r.Ctx.Err() != nil {
return 0, io.EOF
}
<-r.Ctx.Done()
return 0, io.EOF
}
// ReadWriter implements io.ReadWriter. It does not return from Read and discards everything on Write.