@@ -45,7 +45,7 @@ public open class SentryOkHttpEventListener(
4545 private val scopes : IScopes = ScopesAdapter .getInstance(),
4646 private val originalEventListenerCreator : ((call: Call ) -> EventListener )? = null ,
4747) : EventListener() {
48- private val originalEventListenerMap: ConcurrentHashMap <Call , EventListener > = ConcurrentHashMap ()
48+ private val originalEventListenerMap: MutableMap <Call , EventListener > = ConcurrentHashMap ()
4949
5050 // Set only by the constructors that wrap a single EventListener instance. Such a listener is
5151 // shared by every Call anyway, exactly like OkHttp's own EventListener.asFactory(), so it
@@ -95,8 +95,12 @@ public open class SentryOkHttpEventListener(
9595
9696 override fun callStart (call : Call ) {
9797 // The EventListener.Factory contract binds a listener to a single call, so the wrapped
98- // listener is kept per call instead of in a field shared by all concurrent calls
99- val originalEventListener = getOrCreateEventListener(call)
98+ // listener is kept per call instead of in a field shared by all concurrent calls. callStart()
99+ // is invoked only once per Call, so this creates exactly one listener per Call.
100+ val originalEventListener = originalEventListenerCreator?.invoke(call)
101+ if (originalEventListener != null ) {
102+ originalEventListenerMap[call] = originalEventListener
103+ }
100104 originalEventListener?.callStart(call)
101105 // If the wrapped EventListener is ours, we can just delegate the calls,
102106 // without creating other events that would create duplicates
@@ -441,13 +445,6 @@ public open class SentryOkHttpEventListener(
441445 originalEventListenerMap[call]?.cacheConditionalHit(call, cachedResponse)
442446 }
443447
444- // computeIfAbsent, so that concurrent callbacks cannot make the Factory produce two listeners
445- // for the same Call
446- private fun getOrCreateEventListener (call : Call ): EventListener ? {
447- val creator = originalEventListenerCreator ? : return null
448- return originalEventListenerMap.computeIfAbsent(call) { creator.invoke(it) }
449- }
450-
451448 private fun canCreateEventSpan (originalEventListener : EventListener ? ): Boolean {
452449 // If the wrapped EventListener is ours, we shouldn't create spans, as the originalEventListener
453450 // already did it
0 commit comments