Can't run concurrent tor instances.
This slightly modified example crash instantly :
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"time"
"sync"
"github.com/cretz/bine/tor"
"github.com/ipsn/go-libtor"
)
func main() {
var wg sync.WaitGroup
wg.Add(1)
go func(){
defer wg.Done()
handle()
}()
handle()
wg.Wait()
}
func handle() {
// Start tor with some defaults + elevated verbosity
fmt.Println("Starting and registering onion service, please wait a bit...")
t, err := tor.Start(nil, &tor.StartConf{ProcessCreator: libtor.Creator, DebugWriter: os.Stderr})
if err != nil {
log.Panicf("Failed to start tor: %v", err)
}
defer t.Close()
// Wait at most a few minutes to publish the service
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
// Create an onion service to listen on any port but show as 80
onion, err := t.Listen(ctx, &tor.ListenConf{RemotePorts: []int{80}})
if err != nil {
log.Panicf("Failed to create onion service: %v", err)
}
defer onion.Close()
fmt.Printf("Please open a Tor capable browser and navigate to http://%v.onion\n", onion.ID)
// Run a Hello-World HTTP service until terminated
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, Tor!")
})
http.Serve(onion, nil)
}
Crash like this :
Starting and registering onion service, please wait a bit...
Starting and registering onion service, please wait a bit...
Created temp data directory at: /tmp/test/data-dir-257201389
Created temp data directory at: /tmp/test/data-dir-484865384
Starting tor with args [--DataDirectory /tmp/test/data-dir-257201389 --CookieAuthentication 1 --DisableNetwork 1 --hush --SocksPort auto -f /tmp/test/data-dir-257201389/torrc-262615463 --ControlPort auto --ControlPortWriteToFile /tmp/test/data-dir-257201389/control-port-879037146]
Starting tor with args [--DataDirectory /tmp/test/data-dir-484865384 --CookieAuthentication 1 --DisableNetwork 1 --hush --SocksPort auto -f /tmp/test/data-dir-484865384/torrc-925719665 --ControlPort auto --ControlPortWriteToFile /tmp/test/data-dir-484865384/control-port-597338908]
Sep 07 09:22:25.062 [err] tor_assertion_failed_(): Bug: /home/hugo/go/pkg/mod/github.com/ipsn/[email protected]/libtor/../tor/src/../src/feature/hs/hs_circuitmap.c:571: hs_circuitmap_init: Assertion !the_hs_circuitmap failed; aborting. (on Tor 0.3.5.11-dev da728e36f4579907)
Sep 07 09:22:25.063 [err] Bug: Tor 0.3.5.11-dev (git-da728e36f4579907): Assertion !the_hs_circuitmap failed in hs_circuitmap_init at /home/hugo/go/pkg/mod/github.com/ipsn/[email protected]/libtor/../tor/src/../src/feature/hs/hs_circuitmap.c:571. (Stack trace not available) (on Tor 0.3.5.11-dev da728e36f4579907)
SIGABRT: abort
PC=0x7f30fe3b63eb m=0 sigcode=18446744073709551610
goroutine 0 [idle]:
runtime: unknown pc 0x7f30fe3b63eb
stack: frame={sp:0x7ffff8861ae0, fp:0x0} stack=[0x7ffff8062ff8,0x7ffff8862030)
00007ffff88619e0: 0000008000000000 31c8ff87c90f9400
00007ffff88619f0: 0000000000000000 0000000000000000
00007ffff8861a00: 00007ffff8861bd0 00007ffff8861a90
00007ffff8861a10: ffffffffffffffff 0000000000000000
00007ffff8861a20: 00007ffff8861a60 000000000092dfc3
00007ffff8861a30: 00f2892dffffffff 001d9dd8da0f6c3e
00007ffff8861a40: 00479b152ae2f4a4 00007f30fe55c1a0
00007ffff8861a50: 0000000000000000 00000000009303c0
00007ffff8861a60: 7462696c2d6f672f 2e302e317640726f
00007ffff8861a70: 2e2e2f6372732f72 6165662f6372732f
00007ffff8861a80: 3a632e70616d7469 745328202e313735
00007ffff8861a90: 29656c62616c6961 726f54206e6f2820
00007ffff8861aa0: 726f54203a677542 312e352e332e3020
00007ffff8861ab0: 31312e352e332e30 376164207665642d
00007ffff8861ac0: 6563617274206b63 61766120746f6e20
00007ffff8861ad0: 682f73682f657275 6975637269635f73
00007ffff8861ae0: <0000000000000000 726f742f2e2e2f72
00007ffff8861af0: 0000000000000000 0000000000000000
00007ffff8861b00: 0000000000000000 0000000000000000
00007ffff8861b10: 0000000000000000 0000000000000000
00007ffff8861b20: 0000000000ab4a1c 000000000000000d
00007ffff8861b30: 0000000000ab25fd 0000000000000004
00007ffff8861b40: 0000000100000001 0000000100000001
00007ffff8861b50: 0000000000000000 0000000000000000
00007ffff8861b60: fffffffe7fffffff ffffffffffffffff
00007ffff8861b70: ffffffffffffffff ffffffffffffffff
00007ffff8861b80: ffffffffffffffff ffffffffffffffff
00007ffff8861b90: ffffffffffffffff ffffffffffffffff
00007ffff8861ba0: ffffffffffffffff ffffffffffffffff
00007ffff8861bb0: ffffffffffffffff ffffffffffffffff
00007ffff8861bc0: ffffffffffffffff ffffffffffffffff
00007ffff8861bd0: ffffffffffffffff ffffffffffffffff
runtime: unknown pc 0x7f30fe3b63eb
stack: frame={sp:0x7ffff8861ae0, fp:0x0} stack=[0x7ffff8062ff8,0x7ffff8862030)
00007ffff88619e0: 0000008000000000 31c8ff87c90f9400
00007ffff88619f0: 0000000000000000 0000000000000000
00007ffff8861a00: 00007ffff8861bd0 00007ffff8861a90
00007ffff8861a10: ffffffffffffffff 0000000000000000
00007ffff8861a20: 00007ffff8861a60 000000000092dfc3
00007ffff8861a30: 00f2892dffffffff 001d9dd8da0f6c3e
00007ffff8861a40: 00479b152ae2f4a4 00007f30fe55c1a0
00007ffff8861a50: 0000000000000000 00000000009303c0
00007ffff8861a60: 7462696c2d6f672f 2e302e317640726f
00007ffff8861a70: 2e2e2f6372732f72 6165662f6372732f
00007ffff8861a80: 3a632e70616d7469 745328202e313735
00007ffff8861a90: 29656c62616c6961 726f54206e6f2820
00007ffff8861aa0: 726f54203a677542 312e352e332e3020
00007ffff8861ab0: 31312e352e332e30 376164207665642d
00007ffff8861ac0: 6563617274206b63 61766120746f6e20
00007ffff8861ad0: 682f73682f657275 6975637269635f73
00007ffff8861ae0: <0000000000000000 726f742f2e2e2f72
00007ffff8861af0: 0000000000000000 0000000000000000
00007ffff8861b00: 0000000000000000 0000000000000000
00007ffff8861b10: 0000000000000000 0000000000000000
00007ffff8861b20: 0000000000ab4a1c 000000000000000d
00007ffff8861b30: 0000000000ab25fd 0000000000000004
00007ffff8861b40: 0000000100000001 0000000100000001
00007ffff8861b50: 0000000000000000 0000000000000000
00007ffff8861b60: fffffffe7fffffff ffffffffffffffff
00007ffff8861b70: ffffffffffffffff ffffffffffffffff
00007ffff8861b80: ffffffffffffffff ffffffffffffffff
00007ffff8861b90: ffffffffffffffff ffffffffffffffff
00007ffff8861ba0: ffffffffffffffff ffffffffffffffff
00007ffff8861bb0: ffffffffffffffff ffffffffffffffff
00007ffff8861bc0: ffffffffffffffff ffffffffffffffff
00007ffff8861bd0: ffffffffffffffff ffffffffffffffff
goroutine 7 [syscall]:
runtime.cgocall(0x73c9c0, 0xc00004cf28, 0x10)
/usr/local/go/src/runtime/cgocall.go:133 +0x5b fp=0xc00004cef8 sp=0xc00004cec0 pc=0x4a0c1b
github.com/ipsn/go-libtor/libtor._Cfunc_tor_run_main(0x2072820, 0x0)
_cgo_gotypes.go:182 +0x49 fp=0xc00004cf28 sp=0xc00004cef8 pc=0x706249
github.com/ipsn/go-libtor/libtor.(*embeddedProcess).Start.func5.3(0xc000070380, 0xc000012d40)
/home/hugo/go/pkg/mod/github.com/ipsn/[email protected]/libtor/libtor.go:95 +0x56 fp=0xc00004cf60 sp=0xc00004cf28 pc=0x707096
github.com/ipsn/go-libtor/libtor.(*embeddedProcess).Start.func5(0x2072850, 0xc000150100, 0x10, 0x10, 0xc000070380)
/home/hugo/go/pkg/mod/github.com/ipsn/[email protected]/libtor/libtor.go:95 +0x94 fp=0xc00004cfb8 sp=0xc00004cf60 pc=0x707154
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc00004cfc0 sp=0xc00004cfb8 pc=0x506b81
created by github.com/ipsn/go-libtor/libtor.(*embeddedProcess).Start
/home/hugo/go/pkg/mod/github.com/ipsn/[email protected]/libtor/libtor.go:92 +0x225
goroutine 1 [sleep]:
time.Sleep(0xbebc200)
/usr/local/go/src/runtime/time.go:188 +0xbf
github.com/cretz/bine/tor.(*Tor).startProcess(0xc00014c100, 0xb0eba0, 0xc0000160f0, 0xc000157e80, 0xc00005bc88, 0x1)
/home/hugo/go/pkg/mod/github.com/cretz/[email protected]/tor/tor.go:333 +0x5cc
github.com/cretz/bine/tor.Start(0xb0eba0, 0xc0000160f0, 0xc000157e80, 0x1, 0x1, 0x3d)
/home/hugo/go/pkg/mod/github.com/cretz/[email protected]/tor/tor.go:174 +0x4ac
main.handle()
/tmp/test/main.go:30 +0x12a
main.main()
/tmp/test/main.go:23 +0x70
goroutine 6 [sleep]:
time.Sleep(0xbebc200)
/usr/local/go/src/runtime/time.go:188 +0xbf
github.com/cretz/bine/tor.(*Tor).startProcess(0xc000092000, 0xb0eba0, 0xc0000160f0, 0xc000153ed0, 0xc000056cd8, 0x1)
/home/hugo/go/pkg/mod/github.com/cretz/[email protected]/tor/tor.go:333 +0x5cc
github.com/cretz/bine/tor.Start(0xb0eba0, 0xc0000160f0, 0xc000153ed0, 0x1, 0x1, 0x3d)
/home/hugo/go/pkg/mod/github.com/cretz/[email protected]/tor/tor.go:174 +0x4ac
main.handle()
/tmp/test/main.go:30 +0x12a
main.main.func1(0xc000016390)
/tmp/test/main.go:21 +0x4a
created by main.main
/tmp/test/main.go:19 +0x6b
goroutine 18 [syscall]:
github.com/ipsn/go-libtor/libtor._Cfunc_tor_run_main(0x7f30c8000c90, 0x7f3000000000)
_cgo_gotypes.go:182 +0x49
github.com/ipsn/go-libtor/libtor.(*embeddedProcess).Start.func5.3(0xc00009e080, 0xc000098130)
/home/hugo/go/pkg/mod/github.com/ipsn/[email protected]/libtor/libtor.go:95 +0x56
github.com/ipsn/go-libtor/libtor.(*embeddedProcess).Start.func5(0x7f30c8000cc0, 0xc0000a0100, 0x10, 0x10, 0xc00009e080)
/home/hugo/go/pkg/mod/github.com/ipsn/[email protected]/libtor/libtor.go:95 +0x94
created by github.com/ipsn/go-libtor/libtor.(*embeddedProcess).Start
/home/hugo/go/pkg/mod/github.com/ipsn/[email protected]/libtor/libtor.go:92 +0x225
rax 0x0
rbx 0x7f30fe36d740
rcx 0x7f30fe3b63eb
rdx 0x0
rdi 0x2
rsi 0x7ffff8861ae0
rbp 0x7ffff8861d90
rsp 0x7ffff8861ae0
r8 0x0
r9 0x7ffff8861ae0
r10 0x8
r11 0x246
r12 0x80
r13 0x2072d20
r14 0x2072d20
r15 0x10
rip 0x7f30fe3b63eb
rflags 0x246
cs 0x33
fs 0x0
gs 0x0
Is this a complete impossibility (such as the tor node using some global objects) or something that can be fixed with a good lock somewhere ?
I am running into the same problem. Would be nice to run concurrent tor instance in the future.
@karalabe any update on this problem? I could also try to create a PR if you point me in the right direction
@gtuk this is since all of this code is executed in the same process as go.
The easy fix is for architectures that supports it first fork, creating 2 process, the main one that continues and a new one to execute tor into and then execute Tor. (like the unix execution model expects), so multiple tor process wouldn't mengle everything.
It might also be possible to remap Tor's global variables into TLS (Thread Local Storage), it's the same idea, isolate the globals, ... but using Thread Specific memory, instead of Process one.