2021-09-28 12:42:01 +02:00
|
|
|
package netxlite
|
2021-06-15 11:57:40 +02:00
|
|
|
|
2022-05-15 19:25:27 +02:00
|
|
|
//
|
|
|
|
// I/O extensions
|
|
|
|
//
|
|
|
|
|
2021-06-15 11:57:40 +02:00
|
|
|
import (
|
|
|
|
"context"
|
fix(netxlite): robust {ReadAll,Copy}Context with wrapped io.EOF (#661)
* chore(netxlite): add currently failing test case
This diff introduces a test cases that will fail because of the reason
explained in https://github.com/ooni/probe/issues/1965.
* chore(netxlite/iox_test.go): add failing unit tests
These tests directly show how the Go implementation of ReadAll
and Copy has the issue of checking for io.EOF equality.
* fix(netxlite): make {ReadAll,Copy}Context robust to wrapped io.EOF
The fix is simple: we just need to check for `errors.Is(err, io.EOF)`
after either io.ReadAll or io.Copy has returned. When this condition is
true, we need to convert the error back to `nil` as it ought to be.
While there, observe that the unit tests I committed in the previous
commit are wrongly asserting that the error must be wrapped. This
assertion is not correct, because in both cases we have just ensured
that the returned error is `nil` (i.e., success).
See https://github.com/ooni/probe/issues/1965.
* cleanup: remove previous workaround for wrapped io.EOF
These workarounds were partial, meaning that they would cover some
cases in which the issue occurred but not all of them.
Handling the problem in `netxlite.{ReadAll,Copy}Context` is the
right thing to do _as long as_ we always use these functions instead
of `io.{ReadAll,Copy}`.
This is why it's now important to ensure we clearly mention that
inside of the `CONTRIBUTING.md` guide and to also ensure that we're
not using these functions in the code base.
* fix(urlgetter): repair tests who assumed to see EOF error
Now that we have established that we should normalize EOF when
reading bodies like the stdlib does and now that it's clear why
our behavior diverged from the stdlib, we also need to repair
all the tests that assumed this incorrect behavior.
* fix(all): don't use io{,util}.{Copy,ReadAll}
* feat: add checks to ensure we don't use io.{Copy,ReadAll}
* doc(netxlite): document we know how to deal w/ wrapped io.EOF
* fix(nocopyreadall.bash): add exception for i/n/iox.go
2022-01-12 14:26:10 +01:00
|
|
|
"errors"
|
2021-06-15 11:57:40 +02:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2021-06-15 13:44:28 +02:00
|
|
|
// ReadAllContext is like io.ReadAll but reads r in a
|
2021-06-15 11:57:40 +02:00
|
|
|
// background goroutine. This function will return
|
|
|
|
// earlier if the context is cancelled. In which case
|
2021-09-29 20:21:25 +02:00
|
|
|
// we will continue reading from the reader in the background
|
2021-06-15 11:57:40 +02:00
|
|
|
// goroutine, and we will discard the result. To stop
|
2021-09-29 20:21:25 +02:00
|
|
|
// the long-running goroutine, close the connection
|
|
|
|
// bound to the reader. Until such a connection is closed,
|
|
|
|
// you're leaking the backround goroutine and doing I/O.
|
fix(netxlite): robust {ReadAll,Copy}Context with wrapped io.EOF (#661)
* chore(netxlite): add currently failing test case
This diff introduces a test cases that will fail because of the reason
explained in https://github.com/ooni/probe/issues/1965.
* chore(netxlite/iox_test.go): add failing unit tests
These tests directly show how the Go implementation of ReadAll
and Copy has the issue of checking for io.EOF equality.
* fix(netxlite): make {ReadAll,Copy}Context robust to wrapped io.EOF
The fix is simple: we just need to check for `errors.Is(err, io.EOF)`
after either io.ReadAll or io.Copy has returned. When this condition is
true, we need to convert the error back to `nil` as it ought to be.
While there, observe that the unit tests I committed in the previous
commit are wrongly asserting that the error must be wrapped. This
assertion is not correct, because in both cases we have just ensured
that the returned error is `nil` (i.e., success).
See https://github.com/ooni/probe/issues/1965.
* cleanup: remove previous workaround for wrapped io.EOF
These workarounds were partial, meaning that they would cover some
cases in which the issue occurred but not all of them.
Handling the problem in `netxlite.{ReadAll,Copy}Context` is the
right thing to do _as long as_ we always use these functions instead
of `io.{ReadAll,Copy}`.
This is why it's now important to ensure we clearly mention that
inside of the `CONTRIBUTING.md` guide and to also ensure that we're
not using these functions in the code base.
* fix(urlgetter): repair tests who assumed to see EOF error
Now that we have established that we should normalize EOF when
reading bodies like the stdlib does and now that it's clear why
our behavior diverged from the stdlib, we also need to repair
all the tests that assumed this incorrect behavior.
* fix(all): don't use io{,util}.{Copy,ReadAll}
* feat: add checks to ensure we don't use io.{Copy,ReadAll}
* doc(netxlite): document we know how to deal w/ wrapped io.EOF
* fix(nocopyreadall.bash): add exception for i/n/iox.go
2022-01-12 14:26:10 +01:00
|
|
|
//
|
|
|
|
// As of Go 1.17.6, ReadAllContext additionally deals
|
|
|
|
// with wrapped io.EOF correctly, while io.ReadAll does
|
|
|
|
// not. See https://github.com/ooni/probe/issues/1965.
|
2021-06-15 11:57:40 +02:00
|
|
|
func ReadAllContext(ctx context.Context, r io.Reader) ([]byte, error) {
|
|
|
|
datach, errch := make(chan []byte, 1), make(chan error, 1) // buffers
|
|
|
|
go func() {
|
|
|
|
data, err := io.ReadAll(r)
|
fix(netxlite): robust {ReadAll,Copy}Context with wrapped io.EOF (#661)
* chore(netxlite): add currently failing test case
This diff introduces a test cases that will fail because of the reason
explained in https://github.com/ooni/probe/issues/1965.
* chore(netxlite/iox_test.go): add failing unit tests
These tests directly show how the Go implementation of ReadAll
and Copy has the issue of checking for io.EOF equality.
* fix(netxlite): make {ReadAll,Copy}Context robust to wrapped io.EOF
The fix is simple: we just need to check for `errors.Is(err, io.EOF)`
after either io.ReadAll or io.Copy has returned. When this condition is
true, we need to convert the error back to `nil` as it ought to be.
While there, observe that the unit tests I committed in the previous
commit are wrongly asserting that the error must be wrapped. This
assertion is not correct, because in both cases we have just ensured
that the returned error is `nil` (i.e., success).
See https://github.com/ooni/probe/issues/1965.
* cleanup: remove previous workaround for wrapped io.EOF
These workarounds were partial, meaning that they would cover some
cases in which the issue occurred but not all of them.
Handling the problem in `netxlite.{ReadAll,Copy}Context` is the
right thing to do _as long as_ we always use these functions instead
of `io.{ReadAll,Copy}`.
This is why it's now important to ensure we clearly mention that
inside of the `CONTRIBUTING.md` guide and to also ensure that we're
not using these functions in the code base.
* fix(urlgetter): repair tests who assumed to see EOF error
Now that we have established that we should normalize EOF when
reading bodies like the stdlib does and now that it's clear why
our behavior diverged from the stdlib, we also need to repair
all the tests that assumed this incorrect behavior.
* fix(all): don't use io{,util}.{Copy,ReadAll}
* feat: add checks to ensure we don't use io.{Copy,ReadAll}
* doc(netxlite): document we know how to deal w/ wrapped io.EOF
* fix(nocopyreadall.bash): add exception for i/n/iox.go
2022-01-12 14:26:10 +01:00
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
// See https://github.com/ooni/probe/issues/1965
|
|
|
|
err = nil
|
|
|
|
}
|
2021-06-15 11:57:40 +02:00
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
datach <- data
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case data := <-datach:
|
|
|
|
return data, nil
|
|
|
|
case <-ctx.Done():
|
2021-11-06 17:49:58 +01:00
|
|
|
return nil, NewTopLevelGenericErrWrapper(ctx.Err())
|
2021-06-15 11:57:40 +02:00
|
|
|
case err := <-errch:
|
2021-11-06 17:49:58 +01:00
|
|
|
return nil, NewTopLevelGenericErrWrapper(err)
|
2021-06-15 11:57:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-15 13:44:28 +02:00
|
|
|
// CopyContext is like io.Copy but may terminate earlier
|
|
|
|
// when the context expires. This function has the same
|
|
|
|
// caveats of ReadAllContext regarding the temporary leaking
|
2021-09-29 20:21:25 +02:00
|
|
|
// of the background I/O goroutine.
|
fix(netxlite): robust {ReadAll,Copy}Context with wrapped io.EOF (#661)
* chore(netxlite): add currently failing test case
This diff introduces a test cases that will fail because of the reason
explained in https://github.com/ooni/probe/issues/1965.
* chore(netxlite/iox_test.go): add failing unit tests
These tests directly show how the Go implementation of ReadAll
and Copy has the issue of checking for io.EOF equality.
* fix(netxlite): make {ReadAll,Copy}Context robust to wrapped io.EOF
The fix is simple: we just need to check for `errors.Is(err, io.EOF)`
after either io.ReadAll or io.Copy has returned. When this condition is
true, we need to convert the error back to `nil` as it ought to be.
While there, observe that the unit tests I committed in the previous
commit are wrongly asserting that the error must be wrapped. This
assertion is not correct, because in both cases we have just ensured
that the returned error is `nil` (i.e., success).
See https://github.com/ooni/probe/issues/1965.
* cleanup: remove previous workaround for wrapped io.EOF
These workarounds were partial, meaning that they would cover some
cases in which the issue occurred but not all of them.
Handling the problem in `netxlite.{ReadAll,Copy}Context` is the
right thing to do _as long as_ we always use these functions instead
of `io.{ReadAll,Copy}`.
This is why it's now important to ensure we clearly mention that
inside of the `CONTRIBUTING.md` guide and to also ensure that we're
not using these functions in the code base.
* fix(urlgetter): repair tests who assumed to see EOF error
Now that we have established that we should normalize EOF when
reading bodies like the stdlib does and now that it's clear why
our behavior diverged from the stdlib, we also need to repair
all the tests that assumed this incorrect behavior.
* fix(all): don't use io{,util}.{Copy,ReadAll}
* feat: add checks to ensure we don't use io.{Copy,ReadAll}
* doc(netxlite): document we know how to deal w/ wrapped io.EOF
* fix(nocopyreadall.bash): add exception for i/n/iox.go
2022-01-12 14:26:10 +01:00
|
|
|
//
|
|
|
|
// As of Go 1.17.6, CopyContext additionally deals
|
|
|
|
// with wrapped io.EOF correctly, while io.Copy does
|
|
|
|
// not. See https://github.com/ooni/probe/issues/1965.
|
2021-06-15 13:44:28 +02:00
|
|
|
func CopyContext(ctx context.Context, dst io.Writer, src io.Reader) (int64, error) {
|
|
|
|
countch, errch := make(chan int64, 1), make(chan error, 1) // buffers
|
|
|
|
go func() {
|
|
|
|
count, err := io.Copy(dst, src)
|
fix(netxlite): robust {ReadAll,Copy}Context with wrapped io.EOF (#661)
* chore(netxlite): add currently failing test case
This diff introduces a test cases that will fail because of the reason
explained in https://github.com/ooni/probe/issues/1965.
* chore(netxlite/iox_test.go): add failing unit tests
These tests directly show how the Go implementation of ReadAll
and Copy has the issue of checking for io.EOF equality.
* fix(netxlite): make {ReadAll,Copy}Context robust to wrapped io.EOF
The fix is simple: we just need to check for `errors.Is(err, io.EOF)`
after either io.ReadAll or io.Copy has returned. When this condition is
true, we need to convert the error back to `nil` as it ought to be.
While there, observe that the unit tests I committed in the previous
commit are wrongly asserting that the error must be wrapped. This
assertion is not correct, because in both cases we have just ensured
that the returned error is `nil` (i.e., success).
See https://github.com/ooni/probe/issues/1965.
* cleanup: remove previous workaround for wrapped io.EOF
These workarounds were partial, meaning that they would cover some
cases in which the issue occurred but not all of them.
Handling the problem in `netxlite.{ReadAll,Copy}Context` is the
right thing to do _as long as_ we always use these functions instead
of `io.{ReadAll,Copy}`.
This is why it's now important to ensure we clearly mention that
inside of the `CONTRIBUTING.md` guide and to also ensure that we're
not using these functions in the code base.
* fix(urlgetter): repair tests who assumed to see EOF error
Now that we have established that we should normalize EOF when
reading bodies like the stdlib does and now that it's clear why
our behavior diverged from the stdlib, we also need to repair
all the tests that assumed this incorrect behavior.
* fix(all): don't use io{,util}.{Copy,ReadAll}
* feat: add checks to ensure we don't use io.{Copy,ReadAll}
* doc(netxlite): document we know how to deal w/ wrapped io.EOF
* fix(nocopyreadall.bash): add exception for i/n/iox.go
2022-01-12 14:26:10 +01:00
|
|
|
if errors.Is(err, io.EOF) {
|
|
|
|
// See https://github.com/ooni/probe/issues/1965
|
|
|
|
err = nil
|
|
|
|
}
|
2021-06-15 13:44:28 +02:00
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
countch <- count
|
|
|
|
}()
|
|
|
|
select {
|
|
|
|
case count := <-countch:
|
|
|
|
return count, nil
|
|
|
|
case <-ctx.Done():
|
2021-11-06 17:49:58 +01:00
|
|
|
return 0, NewTopLevelGenericErrWrapper(ctx.Err())
|
2021-06-15 13:44:28 +02:00
|
|
|
case err := <-errch:
|
2021-11-06 17:49:58 +01:00
|
|
|
return 0, NewTopLevelGenericErrWrapper(err)
|
2021-06-15 13:44:28 +02:00
|
|
|
}
|
|
|
|
}
|