[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [Git][tpo/applications/tor-browser][tor-browser-102.9.0esr-12.5-1] fixup! Bug 40933: Add tor-launcher functionality



Title: GitLab

Pier Angelo Vendrame pushed to branch tor-browser-102.9.0esr-12.5-1 at The Tor Project / Applications / Tor Browser

Commits:

  • 6dfea410
    by Pier Angelo Vendrame at 2023-04-04T18:32:53+02:00
    fixup! Bug 40933: Add tor-launcher functionality
    
    Bug 41709: Reimplement the failure logic for
    TorProtocolService.sendCommand
    
    sendCommand tries to send a command forever every 250ms in case of
    failure. That is bad because some async code branch might be stuck in it
    forever.
    
    We should have a number of maximum attempts and increase the delay after
    which we try again.
    

2 changed files:

Changes:

  • toolkit/components/tor-launcher/TorMonitorService.jsm
    ... ... @@ -233,8 +233,8 @@ const TorMonitorService = {
    233 233
     
    
    234 234
             // FIXME: TorProcess is misleading here. We should use a topic related
    
    235 235
             // to having a control port connection, instead.
    
    236
    +        logger.info(`Notifying ${TorTopics.ProcessIsReady}`);
    
    236 237
             Services.obs.notifyObservers(null, TorTopics.ProcessIsReady);
    
    237
    -        logger.info(`Notified ${TorTopics.ProcessIsReady}`);
    
    238 238
     
    
    239 239
             // We reset this here hoping that _shutDownEventMonitor can interrupt
    
    240 240
             // the current monitor, either by calling clearTimeout and preventing it
    

  • toolkit/components/tor-launcher/TorProtocolService.jsm
    ... ... @@ -220,49 +220,29 @@ const TorProtocolService = {
    220 220
       // Executes a command on the control port.
    
    221 221
       // Return a reply object or null if a fatal error occurs.
    
    222 222
       async sendCommand(cmd, args) {
    
    223
    -    let conn, reply;
    
    224
    -    const maxAttempts = 2;
    
    225
    -    for (let attempt = 0; !reply && attempt < maxAttempts; attempt++) {
    
    226
    -      try {
    
    227
    -        conn = await this._getConnection();
    
    228
    -        try {
    
    229
    -          if (conn) {
    
    230
    -            reply = await conn.sendCommand(cmd + (args ? " " + args : ""));
    
    231
    -            if (reply) {
    
    232
    -              // Return for reuse.
    
    233
    -              this._returnConnection();
    
    234
    -            } else {
    
    235
    -              // Connection is bad.
    
    236
    -              logger.warn(
    
    237
    -                "sendCommand returned an empty response, taking the connection as broken and closing it."
    
    238
    -              );
    
    239
    -              this._closeConnection();
    
    240
    -            }
    
    241
    -          }
    
    242
    -        } catch (e) {
    
    243
    -          logger.error(`Cannot send the command ${cmd}`, e);
    
    244
    -          this._closeConnection();
    
    245
    -        }
    
    246
    -      } catch (e) {
    
    247
    -        logger.error("Cannot get a connection to the control port", e);
    
    223
    +    const maxTimeout = 1000;
    
    224
    +    let leftConnAttempts = 5;
    
    225
    +    let timeout = 250;
    
    226
    +    let reply;
    
    227
    +    while (leftConnAttempts-- > 0) {
    
    228
    +      const response = await this._trySend(cmd, args, leftConnAttempts == 0);
    
    229
    +      if (response.connected) {
    
    230
    +        reply = response.reply;
    
    231
    +        break;
    
    248 232
           }
    
    249
    -    }
    
    250
    -
    
    251
    -    // We failed to acquire the controller after multiple attempts.
    
    252
    -    // Try again after some time.
    
    253
    -    if (!conn) {
    
    254
    -      logger.info(
    
    255
    -        "sendCommand: Acquiring control connection failed",
    
    233
    +      // We failed to acquire the controller after multiple attempts.
    
    234
    +      // Try again after some time.
    
    235
    +      logger.warn(
    
    236
    +        "sendCommand: Acquiring control connection failed, trying again later.",
    
    256 237
             cmd,
    
    257 238
             args
    
    258 239
           );
    
    259
    -      return new Promise(resolve =>
    
    260
    -        setTimeout(() => {
    
    261
    -          resolve(this.sendCommand(cmd, args));
    
    262
    -        }, 250)
    
    263
    -      );
    
    240
    +      await new Promise(resolve => setTimeout(() => resolve(), timeout));
    
    241
    +      timeout = Math.min(2 * timeout, maxTimeout);
    
    264 242
         }
    
    265 243
     
    
    244
    +    // We sent the command, but we still got an empty response.
    
    245
    +    // Something must be busted elsewhere.
    
    266 246
         if (!reply) {
    
    267 247
           throw new Error(`${cmd} sent an empty response`);
    
    268 248
         }
    
    ... ... @@ -590,6 +570,48 @@ const TorProtocolService = {
    590 570
         }
    
    591 571
       },
    
    592 572
     
    
    573
    +  async _trySend(cmd, args, rethrow) {
    
    574
    +    let connected = false;
    
    575
    +    let reply;
    
    576
    +    let leftAttempts = 2;
    
    577
    +    while (leftAttempts-- > 0) {
    
    578
    +      let conn;
    
    579
    +      try {
    
    580
    +        conn = await this._getConnection();
    
    581
    +      } catch (e) {
    
    582
    +        logger.error("Cannot get a connection to the control port", e);
    
    583
    +        if (leftAttempts == 0 && rethrow) {
    
    584
    +          throw e;
    
    585
    +        }
    
    586
    +      }
    
    587
    +      if (!conn) {
    
    588
    +        continue;
    
    589
    +      }
    
    590
    +      // If we _ever_ got a connection, the caller should not try again
    
    591
    +      connected = true;
    
    592
    +      try {
    
    593
    +        reply = await conn.sendCommand(cmd + (args ? " " + args : ""));
    
    594
    +        if (reply) {
    
    595
    +          // Return for reuse.
    
    596
    +          this._returnConnection();
    
    597
    +        } else {
    
    598
    +          // Connection is bad.
    
    599
    +          logger.warn(
    
    600
    +            "sendCommand returned an empty response, taking the connection as broken and closing it."
    
    601
    +          );
    
    602
    +          this._closeConnection();
    
    603
    +        }
    
    604
    +      } catch (e) {
    
    605
    +        logger.error(`Cannot send the command ${cmd}`, e);
    
    606
    +        this._closeConnection();
    
    607
    +        if (leftAttempts == 0 && rethrow) {
    
    608
    +          throw e;
    
    609
    +        }
    
    610
    +      }
    
    611
    +    }
    
    612
    +    return { connected, reply };
    
    613
    +  },
    
    614
    +
    
    593 615
       // Opens an authenticated connection, sets it to this._controlConnection, and
    
    594 616
       // return it.
    
    595 617
       async _getConnection() {
    

  • _______________________________________________
    tor-commits mailing list
    tor-commits@xxxxxxxxxxxxxxxxxxxx
    https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits