Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Fix error branches, take 3. #14721

Merged
merged 5 commits into from
Jul 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ function setupTor () {
return
}
torDaemon.on('exit', () => onTorError('The Tor process has exited.'))
torDaemon.on('error', (err) => onTorError(`${err}`))
torDaemon.on('launch', (socksAddr) => {
const version = torDaemon.getVersion()
console.log(`tor: daemon listens on ${socksAddr}, version ${version}`)
Expand Down
47 changes: 30 additions & 17 deletions app/tor.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,18 @@ class TorDaemon extends EventEmitter {
}

/**
* Kill the tor daemon.
*
* Idempotent. Repeated calls have no effect.
* Stop watching for the tor daemon to start up.
*/
kill () {
if (this._watcher === null) {
return
}
stop () {
assert(this._watcher)
this._watcher.close()
this._watcher = null
}

/**
* Kill the tor daemon and close the control channel.
*/
kill () {
if (!this._process) {
assert(this._process === null)
assert(this._control === null)
Expand All @@ -176,7 +177,6 @@ class TorDaemon extends EventEmitter {
* @param {string} msg - error message
*/
_error (msg) {
console.log(msg)
this.emit('error', msg)
this.kill()
}
Expand Down Expand Up @@ -311,6 +311,7 @@ class TorDaemon extends EventEmitter {
_polled () {
assert(this._polling)
if (this._retry_poll && this._control === null) {
this._retry_poll = false
return process.nextTick(() => this._doPoll())
}
this._polling = false
Expand Down Expand Up @@ -525,16 +526,16 @@ class TorDaemon extends EventEmitter {
}
}
if (err) {
return this._error(`tor: authentication failure: ${err}`)
return this._error(`authentication failure: ${err}`)
}
this._control.getSOCKSListeners((err, listeners) => {
if (err) {
return this._error(`tor: failed to get socks addresses: ${err}`)
return this._error(`failed to get socks addresses: ${err}`)
}
this._socks_addresses = listeners
this._control.getVersion((err, version) => {
if (err) {
return this._error(`tor: failed to get version: ${err}`)
return this._error(`failed to get version: ${err}`)
}
this._tor_version = version
this.emit('launch', this.getSOCKSAddress())
Expand Down Expand Up @@ -983,6 +984,7 @@ class TorControl extends EventEmitter {
this._writable.removeListener('error', this._writable_on_error)
this._writable.removeListener('close', this._writable_on_close)
this._destroyed = true
err = err || new Error('tor: control channel destroyed')
while (this._cmdq.length > 0) {
const [, callback] = this._cmdq.shift()
callback(err, null, null)
Expand Down Expand Up @@ -1134,20 +1136,29 @@ class TorControl extends EventEmitter {
_onEnd () {
assert(!this._destroyed)
if (this._cmdq.length > 0) {
this._error('tor: control connection closed prematurely')
this._error('control connection closed prematurely')
}
this.emit('end')
}

/**
* Internal subroutine. Callback for errors on the enclosed
* readable or writable. Pass it along.
* readable or writable. If there are any commands pending, fail
* them with this error. Otherwise, emit an asynchronous 'error'
* event.
*
* @param {Error} err
*/
_onError (err) {
assert(!this._destroyed)
this.emit('error', err)
if (this._cmdq.length > 0) {
assert(!this._destroyed)
do {
const [, callback] = this._cmdq.shift()
callback(err, null, null)
} while (this._cmdq.length > 0)
} else {
this.emit('error', err)
}
}

/**
Expand All @@ -1166,12 +1177,14 @@ class TorControl extends EventEmitter {
}

/**
* Internal subroutine. Emit an error with a prescribed message.
* Internal subroutine. Pass an error with the prescribed message
* to any callbacks, or emit an error with a prescribed message if
* there are none.
*
* @param {string} msg
*/
_error (msg) {
this.emit('error', new Error(msg))
this._onError(new Error(msg))
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/integration/app/torTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe('tor unit tests', function () {
torProcess = null
}
assert(torDaemon)
torDaemon.stop()
torDaemon.kill()
torDaemon = null
rimraf(bravePath(), (err) => {
Expand Down