@@ -1566,8 +1566,8 @@ _mongoc_client_retryable_write_command_with_stream (
1566
1566
mongoc_server_stream_cleanup (retry_server_stream );
1567
1567
}
1568
1568
1569
- retry_server_stream =
1570
- mongoc_cluster_stream_for_writes ( & client -> cluster , & ignored_error );
1569
+ retry_server_stream = mongoc_cluster_stream_for_writes (
1570
+ & client -> cluster , parts -> assembled . session , NULL , & ignored_error );
1571
1571
1572
1572
if (retry_server_stream &&
1573
1573
retry_server_stream -> sd -> max_wire_version >=
@@ -1645,16 +1645,13 @@ mongoc_client_command_simple (mongoc_client_t *client,
1645
1645
* preference argument."
1646
1646
*/
1647
1647
server_stream =
1648
- mongoc_cluster_stream_for_reads (cluster , read_prefs , NULL , error );
1648
+ mongoc_cluster_stream_for_reads (cluster , read_prefs , NULL , reply , error );
1649
1649
1650
1650
if (server_stream ) {
1651
1651
ret = _mongoc_client_command_with_stream (
1652
1652
client , & parts , server_stream , reply , error );
1653
1653
} else {
1654
- if (reply ) {
1655
- bson_init (reply );
1656
- }
1657
-
1654
+ /* reply initialized by mongoc_cluster_stream_for_reads */
1658
1655
ret = false;
1659
1656
}
1660
1657
@@ -1713,10 +1710,12 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1713
1710
const mongoc_read_prefs_t * prefs = COALESCE (user_prefs , default_prefs );
1714
1711
mongoc_server_stream_t * server_stream = NULL ;
1715
1712
mongoc_cluster_t * cluster ;
1713
+ mongoc_client_session_t * cs ;
1716
1714
bson_t reply_local ;
1717
1715
bson_t * reply_ptr ;
1718
1716
int32_t wire_version ;
1719
1717
int32_t wc_wire_version ;
1718
+ bool reply_initialized = false;
1720
1719
bool ret = false;
1721
1720
1722
1721
ENTRY ;
@@ -1725,22 +1724,26 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1725
1724
BSON_ASSERT (db_name );
1726
1725
BSON_ASSERT (command );
1727
1726
1727
+ command_name = _mongoc_get_command_name (command );
1728
+ cluster = & client -> cluster ;
1729
+ reply_ptr = reply ? reply : & reply_local ;
1730
+
1728
1731
mongoc_cmd_parts_init (& parts , client , db_name , flags , command );
1729
1732
parts .is_read_command = (mode & MONGOC_CMD_READ );
1730
1733
parts .is_write_command = (mode & MONGOC_CMD_WRITE );
1731
1734
1732
1735
if (!_mongoc_read_write_opts_parse (client , opts , & read_write_opts , error )) {
1733
- GOTO (err );
1736
+ GOTO (done );
1734
1737
}
1735
1738
1736
- command_name = _mongoc_get_command_name ( command ) ;
1739
+ cs = read_write_opts . client_session ;
1737
1740
1738
1741
if (!command_name ) {
1739
1742
bson_set_error (error ,
1740
1743
MONGOC_ERROR_COMMAND ,
1741
1744
MONGOC_ERROR_COMMAND_INVALID_ARG ,
1742
1745
"Empty command document" );
1743
- GOTO (err );
1746
+ GOTO (done );
1744
1747
}
1745
1748
1746
1749
if (_mongoc_client_session_in_txn (read_write_opts .client_session )) {
@@ -1750,15 +1753,15 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1750
1753
MONGOC_ERROR_COMMAND ,
1751
1754
MONGOC_ERROR_COMMAND_INVALID_ARG ,
1752
1755
"Read preference in a transaction must be primary" );
1753
- GOTO (err );
1756
+ GOTO (done );
1754
1757
}
1755
1758
1756
1759
if (!bson_empty (& read_write_opts .readConcern )) {
1757
1760
bson_set_error (error ,
1758
1761
MONGOC_ERROR_COMMAND ,
1759
1762
MONGOC_ERROR_COMMAND_INVALID_ARG ,
1760
1763
"Cannot set read concern after starting transaction" );
1761
- GOTO (err );
1764
+ GOTO (done );
1762
1765
}
1763
1766
1764
1767
if (read_write_opts .writeConcern &&
@@ -1768,16 +1771,14 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1768
1771
MONGOC_ERROR_COMMAND ,
1769
1772
MONGOC_ERROR_COMMAND_INVALID_ARG ,
1770
1773
"Cannot set write concern after starting transaction" );
1771
- GOTO (err );
1774
+ GOTO (done );
1772
1775
}
1773
1776
}
1774
1777
1775
- reply_ptr = reply ? reply : & reply_local ;
1776
-
1777
1778
if (mode == MONGOC_CMD_READ || mode == MONGOC_CMD_RAW ) {
1778
1779
/* NULL read pref is ok */
1779
1780
if (!_mongoc_read_prefs_validate (prefs , error )) {
1780
- GOTO (err );
1781
+ GOTO (done );
1781
1782
}
1782
1783
1783
1784
parts .read_prefs = prefs ;
@@ -1786,31 +1787,37 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1786
1787
prefs = NULL ;
1787
1788
}
1788
1789
1789
- cluster = & client -> cluster ;
1790
-
1791
1790
if (read_write_opts .serverId ) {
1792
1791
/* "serverId" passed in opts */
1793
- server_stream = mongoc_cluster_stream_for_server (
1794
- cluster , read_write_opts .serverId , true /* reconnect ok */ , error );
1792
+ server_stream =
1793
+ mongoc_cluster_stream_for_server (cluster ,
1794
+ read_write_opts .serverId ,
1795
+ true /* reconnect ok */ ,
1796
+ cs ,
1797
+ reply_ptr ,
1798
+ error );
1795
1799
1796
1800
if (server_stream && server_stream -> sd -> type != MONGOC_SERVER_MONGOS ) {
1797
1801
parts .user_query_flags |= MONGOC_QUERY_SLAVE_OK ;
1798
1802
}
1799
1803
} else if (parts .is_write_command ) {
1800
- server_stream = mongoc_cluster_stream_for_writes (cluster , error );
1804
+ server_stream =
1805
+ mongoc_cluster_stream_for_writes (cluster , cs , reply_ptr , error );
1801
1806
} else {
1802
- server_stream = mongoc_cluster_stream_for_reads (
1803
- cluster , prefs , read_write_opts . client_session , error );
1807
+ server_stream =
1808
+ mongoc_cluster_stream_for_reads ( cluster , prefs , cs , reply_ptr , error );
1804
1809
}
1805
1810
1806
1811
if (!server_stream ) {
1807
- GOTO (err );
1812
+ /* stream_for_reads/writes/server has initialized reply */
1813
+ reply_initialized = true;
1814
+ GOTO (done );
1808
1815
}
1809
1816
1810
1817
wire_version = server_stream -> sd -> max_wire_version ;
1811
1818
if (!mongoc_cmd_parts_append_read_write (
1812
1819
& parts , & read_write_opts , wire_version , error )) {
1813
- GOTO (err );
1820
+ GOTO (done );
1814
1821
}
1815
1822
1816
1823
if (mode & MONGOC_CMD_WRITE ) {
@@ -1828,7 +1835,7 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1828
1835
command_name ,
1829
1836
wire_version ,
1830
1837
wc_wire_version );
1831
- GOTO (err );
1838
+ GOTO (done );
1832
1839
}
1833
1840
1834
1841
/* use default write concern unless it's in opts */
@@ -1837,7 +1844,7 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1837
1844
wire_version >= wc_wire_version ) {
1838
1845
if (!mongoc_cmd_parts_set_write_concern (
1839
1846
& parts , default_wc , wire_version , error )) {
1840
- GOTO (err );
1847
+ GOTO (done );
1841
1848
}
1842
1849
}
1843
1850
}
@@ -1846,27 +1853,27 @@ _mongoc_client_command_with_opts (mongoc_client_t *client,
1846
1853
if ((mode & MONGOC_CMD_READ ) && bson_empty (& read_write_opts .readConcern )) {
1847
1854
if (!mongoc_cmd_parts_set_read_concern (
1848
1855
& parts , default_rc , wire_version , error )) {
1849
- GOTO (err );
1856
+ GOTO (done );
1850
1857
}
1851
1858
}
1852
1859
1853
1860
ret = _mongoc_client_command_with_stream (
1854
1861
client , & parts , server_stream , reply_ptr , error );
1855
1862
1863
+ reply_initialized = true;
1864
+
1856
1865
if (ret && (mode & MONGOC_CMD_WRITE )) {
1857
1866
ret = !_mongoc_parse_wc_err (reply_ptr , error );
1858
1867
}
1859
- if (reply_ptr == & reply_local ) {
1860
- bson_destroy (reply_ptr );
1861
- }
1862
- GOTO (done );
1863
-
1864
- err :
1865
- if (reply ) {
1866
- bson_init (reply );
1867
- }
1868
1868
1869
1869
done :
1870
+ if (reply_ptr == & reply_local ) {
1871
+ if (reply_initialized ) {
1872
+ bson_destroy (reply_ptr );
1873
+ }
1874
+ } else if (!reply_initialized ) {
1875
+ _mongoc_bson_init_if_set (reply );
1876
+ }
1870
1877
1871
1878
if (server_stream ) {
1872
1879
mongoc_server_stream_cleanup (server_stream );
@@ -2000,7 +2007,7 @@ mongoc_client_command_simple_with_server_id (
2000
2007
}
2001
2008
2002
2009
server_stream = mongoc_cluster_stream_for_server (
2003
- & client -> cluster , server_id , true /* reconnect ok */ , error );
2010
+ & client -> cluster , server_id , true /* reconnect ok */ , NULL , reply , error );
2004
2011
2005
2012
if (server_stream ) {
2006
2013
mongoc_cmd_parts_init (
@@ -2014,10 +2021,7 @@ mongoc_client_command_simple_with_server_id (
2014
2021
mongoc_server_stream_cleanup (server_stream );
2015
2022
RETURN (ret );
2016
2023
} else {
2017
- if (reply ) {
2018
- bson_init (reply );
2019
- }
2020
-
2024
+ /* stream_for_server initialized reply */
2021
2025
RETURN (false);
2022
2026
}
2023
2027
}
@@ -2055,7 +2059,7 @@ _mongoc_client_kill_cursor (mongoc_client_t *client,
2055
2059
2056
2060
/* don't attempt reconnect if server unavailable, and ignore errors */
2057
2061
server_stream = mongoc_cluster_stream_for_server (
2058
- & client -> cluster , server_id , false /* reconnect_ok */ , NULL /* error */ );
2062
+ & client -> cluster , server_id , false /* reconnect_ok */ , NULL , NULL , NULL );
2059
2063
2060
2064
if (!server_stream ) {
2061
2065
return ;
@@ -2759,7 +2763,7 @@ _mongoc_client_end_sessions (mongoc_client_t *client)
2759
2763
}
2760
2764
2761
2765
stream = mongoc_cluster_stream_for_server (
2762
- cluster , server_id , false /* reconnect_ok */ , & error );
2766
+ cluster , server_id , false /* reconnect_ok */ , NULL , NULL , & error );
2763
2767
2764
2768
if (!stream ) {
2765
2769
MONGOC_WARNING ("Couldn't send \"endSessions\": %s" , error .message );
0 commit comments