Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-2341

Segfault destroying client if one of its sessions is not destroyed first

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: libmongoc
    • Labels:
      None

      While implementing retryable write tests (CDRIVER-2228), I encountered a segfault when I forgot to destroy a session:

      mongoc_uri_t *uri;
      mongoc_client_t *client;
      mongoc_client_session_t *session;
      mongoc_collection_t *collection;
      bson_t test;
      bson_error_t error;
      
      uri = test_framework_get_uri ();
      mongoc_uri_set_option_as_bool (uri, "retryWrites", false);
      
      client = mongoc_client_new_from_uri (uri);
      test_framework_set_ssl_opts (client);
      mongoc_uri_destroy (uri);
      
      /* TODO: Remove this once start_session initializes the topology */
      mongoc_client_command_simple (client, "admin", tmp_bson ("{'ping': 1}"), NULL, NULL, &error);
      
      session = mongoc_client_start_session (client, NULL, &error);
      
      collection = get_test_collection (client, "retryable_writes");
      
      // Commenting these out, but these are functions that run the JSON test spec
      // insert_data (collection, scenario);
      // execute_test (collection, &test, session);
      
      if (!mongoc_collection_drop (collection, &error)) {
         if (strcmp (error.message, "ns not found")) {
            /* an error besides ns not found */
            ASSERT_OR_PRINT (false, error);
         }
      }
      
      mongoc_collection_destroy (collection);
      // If the session is not destroyed at all, we get a segfault during client destruction
      // mongoc_client_session_destroy (session);
      mongoc_client_destroy (client);
      

      Backtrace:

      #0  0x0000000000000000 in ?? ()
      #1  0x0000000000502c2c in mongoc_set_for_each (set=0x502c2c <mongoc_set_for_each+38>, cb=0x7ffd52361f50, ctx=0x7fe8bdd4557d <bson_free+28>) at src/mongoc/mongoc-set.c:163
      #2  0x00000000004d1373 in mongoc_client_get_uri (client=0x27a8260) at src/mongoc/mongoc-client.c:1059
      #3  0x000000000048f33e in test_all_spec_tests (suite=0x561707) at tests/test-mongoc-retryable-writes.c:656
      #4  0x00000000004c3b95 in TestSuite_RunTest (suite=0x7ffd523627b0, test=0x275b1b0, count=0x7ffd52362738) at tests/TestSuite.c:698
      #5  0x00000000004c463d in TestSuite_RunNamed (suite=0x7ffd523627b0, testname=0x2736270 "/retryable_writes/*") at tests/TestSuite.c:1021
      #6  0x00000000004c4767 in TestSuite_Destroy (suite=0x7ffd523627b0) at tests/TestSuite.c:1061
      #7  0x0000000000419a45 in main (argc=5, argv=0x7ffd523628f8) at tests/test-libmongoc.c:2147
      

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            jmikola@mongodb.com Jeremy Mikola
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: