Skip to content

Commit bc7599c

Browse files
authored
fix(libtor): avoid data race (#1132)
There's a data race in the `libtor` package, as documented by ooni/probe#2406 (comment). This diff applies the commits mentioned by ooni/probe#2406 (comment).
1 parent 48c6d09 commit bc7599c

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

internal/libtor/enabled.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import (
5757
"fmt"
5858
"net"
5959
"os"
60+
"runtime"
6061
"sync"
6162

6263
"github.com/cretz/bine/process"
@@ -149,6 +150,13 @@ var ErrNonzeroExitCode = errors.New("libtor: command completed with nonzero exit
149150
// runtor runs tor until completion and ensures that tor exits when
150151
// the given ctx is cancelled or its deadline expires.
151152
func (p *torProcess) runtor(ctx context.Context, cc net.Conn, args ...string) {
153+
// make sure we lock to an OS thread otherwise the goroutine can get
154+
// preempted midway and cause data races
155+
//
156+
// See https://github.com/ooni/probe/issues/2406#issuecomment-1479138677
157+
runtime.LockOSThread()
158+
defer runtime.UnlockOSThread()
159+
152160
// wait for Start or context to expire
153161
select {
154162
case <-p.awaitStart:

0 commit comments

Comments
 (0)