Uploaded image for project: 'PHP Driver: Extension'
  1. PHP Driver: Extension
  2. PHPC-2029

Memory leak when saving Server object from command monitoring event subscriber

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: 1.10.0
    • Component/s: None
    • Labels:
      None

      This was discovered while investigating SDAM events for PHPC-2023.

      Saving a Server object from a command monitoring event causes both PHP's debug mode and Valgrind to report a memory leak in the allocation of that Server object. It's not clear if this has anything to do with the underlying Manager reference on the Server and event objects. Also, this issue only appears with per-client event subscribers (PHPC-1666) and not global subscribers. The use of disableClientPersistence also does not appear to be relevant.

      Example test:

      --TEST--
      MongoDB\Driver\Monitoring\CommandStartedEvent saving Server causes leak
      --SKIPIF--
      <?php require __DIR__ . "/../utils/basic-skipif.inc"; ?>
      <?php skip_if_not_live(); ?>
      <?php skip_if_not_clean(); ?>
      --FILE--
      <?php
      require_once __DIR__ . "/../utils/basic.inc";
      
      class MySubscriber implements MongoDB\Driver\Monitoring\CommandSubscriber
      {
          private $server;
      
          public function commandStarted(MongoDB\Driver\Monitoring\CommandStartedEvent $event)
          {
              printf("Observed commandStarted for %s\n", $event->getCommandName());
              $this->server = $event->getServer();
          }
      
          public function commandSucceeded(MongoDB\Driver\Monitoring\CommandSucceededEvent $event) {}
      
          public function commandFailed(MongoDB\Driver\Monitoring\CommandFailedEvent $event) {}
      }
      
      $manager = create_test_manager();
      
      $singleSubscriber = new MySubscriber();
      $manager->addSubscriber($singleSubscriber);
      
      $command = new MongoDB\Driver\Command(['ping' => 1]);
      $manager->executeCommand(DATABASE_NAME, $command);
      
      ?>
      ===DONE===
      --EXPECT--
      Observed commandStarted for ping
      ===DONE===
      

      This issue would likely be avoided (albeit not explicitly solved) by removing Manager references and getServer() methods from command monitoring events (PHPC-1741).

            Assignee:
            Unassigned Unassigned
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: