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

Commit

Permalink
Merge pull request #14526 from brave/riastradh-tor-cookieportorder
Browse files Browse the repository at this point in the history
Fix order of reading notifications for tor launch.
  • Loading branch information
diracdeltas authored Jun 22, 2018
2 parents e420668 + a734d34 commit 4020152
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions app/tor.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class TorDaemon extends EventEmitter {
// Begin watching for the control port file to be written.
const watchDir = torWatchDirPath()
const watchOpts = {persistent: false}
assert(this._watcher === null)
this._watcher = fs.watch(watchDir, watchOpts, (event, filename) => {
this._watchEvent(event, filename)
})
Expand All @@ -150,6 +151,9 @@ class TorDaemon extends EventEmitter {
* Kill the tor daemon.
*/
kill () {
assert(this._watcher)
this._watcher.close()
this._watcher = null
if (!this._process) {
assert(this._process === null)
assert(this._control === null)
Expand All @@ -173,14 +177,15 @@ class TorDaemon extends EventEmitter {
* @param {string} filename
*/
_watchEvent (event, filename) {
assert(this._watcher)

// If the process died in the interim, give up.
if (!this._process) {
console.log('tor: process dead, ignoring watch event')
return
}

// Watcher shouldn't be there without process.
assert(this._watcher)

// If the control connection is already open, nothing to do.
if (this._control) {
return
Expand Down Expand Up @@ -228,7 +233,7 @@ class TorDaemon extends EventEmitter {
assert(this._control === null)
assert(this._polling)

this._eatControlPort((err, portno, portMtime) => {
this._eatControlCookie((err, cookie, cookieMtime) => {
if (err) {
// If there's an error, don't worry: the file may have been
// written incompletely, and we will, with any luck, be notified
Expand All @@ -248,7 +253,7 @@ class TorDaemon extends EventEmitter {
assert(this._control === null)
assert(this._polling)

this._eatControlCookie((err, cookie, cookieMtime) => {
this._eatControlPort((err, portno, portMtime) => {
if (err) {
// If there's an error, don't worry: the file may not be
// ready yet, and we'll be notified when it is.
Expand Down Expand Up @@ -288,7 +293,7 @@ class TorDaemon extends EventEmitter {
*/
_polled () {
assert(this._polling)
if (this._retry_polling && this._control === null) {
if (this._retry_poll && this._control === null) {
return process.nextTick(() => this._doPoll())
}
this._polling = false
Expand Down

0 comments on commit 4020152

Please sign in to comment.