Commit 743eaa8
committed
Summary
I've successfully refactored the code to fix the bug where activity listeners were being checked when the connection was created (at which point none may be set), but not checked again when the connection was pulled from the pool.
Key Changes:
Constructor (CommandEventHelper.cs:60): Removed the listener check from _shouldTrackState initialization. Now _shouldTrackState only depends on whether event tracking is needed (_shouldTrackSucceeded || _shouldTrackFailed), not on tracing with listeners.
Property Getters (CommandEventHelper.cs:71-94): Updated all property getters (ShouldCallBeforeSending, ShouldCallAfterSending, etc.) to dynamically check MongoTelemetry.ActivitySource.HasListeners() at execution time, ensuring that activity tracing is detected when connections are obtained from the pool.
Lazy State Initialization (CommandEventHelper.cs:37, 741-745): Changed _state from readonly to allow lazy initialization. The state dictionary is now only created when needed - either for event tracking or when tracing with listeners is detected at runtime.
Null Safety (CommandEventHelper.cs:132, 165, 192, 216, 233): Added null checks before accessing _state in all methods to handle cases where state tracking hasn't been initialized.
TrackCommandState (CommandEventHelper.cs:734-770): Updated to check for listeners at execution time and lazily initialize the state dictionary only when tracing is actually needed.
Test Update (CommandEventHelperTests.cs:122-124): Updated the test to reflect the new behavior where _shouldTrackState only reflects event tracking, not tracing with listeners (which is now checked dynamically).
Benefits:
Bug Fixed: Activity listeners are now checked when connections are obtained from the pool, not just when they're created
No Unnecessary State Tracking: State tracking is only enabled when actually needed (either for events or when listeners are registered), avoiding the performance overhead of always tracking state
Backward Compatible: The behavior is the same for all scenarios, just more efficient
The changes ensure that the driver properly detects activity listeners whenever a connection is used, whether it's newly created or retrieved from the pool, while avoiding unnecessary state tracking when no listeners are registered.1 parent bb33139 commit 743eaa8
File tree
3 files changed
+58
-39
lines changed- src/MongoDB.Driver/Core/Connections
- tests
- MongoDB.Driver.Tests/Core/Connections
- SmokeTests/MongoDB.Driver.SmokeTests.Sdk
3 files changed
+58
-39
lines changedLines changed: 54 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
| 37 | + | |
38 | 38 | | |
39 | 39 | | |
40 | 40 | | |
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
60 | | - | |
| 60 | + | |
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
72 | | - | |
73 | | - | |
74 | | - | |
| 71 | + | |
75 | 72 | | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
| 73 | + | |
80 | 74 | | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
| 75 | + | |
85 | 76 | | |
86 | | - | |
87 | | - | |
88 | | - | |
89 | | - | |
| 77 | + | |
90 | 78 | | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
95 | 83 | | |
96 | 84 | | |
97 | 85 | | |
| |||
129 | 117 | | |
130 | 118 | | |
131 | 119 | | |
132 | | - | |
133 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
134 | 126 | | |
135 | 127 | | |
136 | 128 | | |
| |||
157 | 149 | | |
158 | 150 | | |
159 | 151 | | |
160 | | - | |
161 | | - | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
162 | 155 | | |
163 | | - | |
| 156 | + | |
| 157 | + | |
164 | 158 | | |
165 | | - | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
166 | 162 | | |
167 | 163 | | |
168 | 164 | | |
| |||
179 | 175 | | |
180 | 176 | | |
181 | 177 | | |
182 | | - | |
183 | | - | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
| |||
198 | 198 | | |
199 | 199 | | |
200 | 200 | | |
201 | | - | |
202 | | - | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
203 | 209 | | |
204 | 210 | | |
205 | 211 | | |
206 | 212 | | |
207 | 213 | | |
208 | 214 | | |
209 | 215 | | |
210 | | - | |
211 | | - | |
212 | 216 | | |
213 | 217 | | |
214 | 218 | | |
| |||
223 | 227 | | |
224 | 228 | | |
225 | 229 | | |
226 | | - | |
| 230 | + | |
227 | 231 | | |
228 | 232 | | |
229 | 233 | | |
230 | 234 | | |
231 | 235 | | |
232 | 236 | | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
233 | 242 | | |
234 | 243 | | |
235 | 244 | | |
| |||
731 | 740 | | |
732 | 741 | | |
733 | 742 | | |
734 | | - | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
735 | 746 | | |
736 | 747 | | |
737 | 748 | | |
738 | 749 | | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
739 | 756 | | |
740 | 757 | | |
741 | 758 | | |
| |||
746 | 763 | | |
747 | 764 | | |
748 | 765 | | |
749 | | - | |
| 766 | + | |
750 | 767 | | |
751 | 768 | | |
752 | 769 | | |
| |||
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
123 | 125 | | |
124 | 126 | | |
125 | 127 | | |
| |||
Lines changed: 1 addition & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
| 62 | + | |
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| |||
0 commit comments