@@ -45,7 +45,12 @@ 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: MutableMap <Call , EventListener > = ConcurrentHashMap ()
48+ private val originalEventListenerMap: ConcurrentHashMap <Call , EventListener > = ConcurrentHashMap ()
49+
50+ // Set only by the constructors that wrap a single EventListener instance. Such a listener is
51+ // shared by every Call anyway, exactly like OkHttp's own EventListener.asFactory(), so it
52+ // exists independently of the callStart()..callEnd() window and can always be delegated to.
53+ private var fixedOriginalEventListener: EventListener ? = null
4954
5055 public companion object {
5156 internal const val PROXY_SELECT_EVENT = " http.client.proxy_select_ms"
@@ -65,7 +70,9 @@ public open class SentryOkHttpEventListener(
6570
6671 public constructor (
6772 originalEventListener: EventListener
68- ) : this (ScopesAdapter .getInstance(), originalEventListenerCreator = { originalEventListener })
73+ ) : this (ScopesAdapter .getInstance(), originalEventListenerCreator = { originalEventListener }) {
74+ fixedOriginalEventListener = originalEventListener
75+ }
6976
7077 public constructor (
7178 originalEventListenerFactory: Factory
@@ -77,7 +84,9 @@ public open class SentryOkHttpEventListener(
7784 public constructor (
7885 scopes: IScopes = ScopesAdapter .getInstance(),
7986 originalEventListener: EventListener ,
80- ) : this (scopes, originalEventListenerCreator = { originalEventListener })
87+ ) : this (scopes, originalEventListenerCreator = { originalEventListener }) {
88+ fixedOriginalEventListener = originalEventListener
89+ }
8190
8291 public constructor (
8392 scopes: IScopes = ScopesAdapter .getInstance(),
@@ -87,10 +96,7 @@ public open class SentryOkHttpEventListener(
8796 override fun callStart (call : Call ) {
8897 // The EventListener.Factory contract binds a listener to a single call, so the wrapped
8998 // listener is kept per call instead of in a field shared by all concurrent calls
90- val originalEventListener = originalEventListenerCreator?.invoke(call)
91- if (originalEventListener != null ) {
92- originalEventListenerMap[call] = originalEventListener
93- }
99+ val originalEventListener = getOrCreateEventListener(call)
94100 originalEventListener?.callStart(call)
95101 // If the wrapped EventListener is ours, we can just delegate the calls,
96102 // without creating other events that would create duplicates
@@ -397,13 +403,17 @@ public open class SentryOkHttpEventListener(
397403 }
398404
399405 override fun canceled (call : Call ) {
400- // Unlike the other callbacks, canceled() can occur before callStart() and after
401- // callEnd()/callFailed(), so there is not always a listener bound to the call. Create one on
402- // the fly in that case, but do not put it in the map: nothing would remove it again, because
403- // a call that is canceled before it starts never gets a callEnd() or callFailed().
406+ // canceled() is not part of the call window: OkHttp may deliver it before callStart() and
407+ // after callEnd()/callFailed(), because it holds the listener for the whole Call lifetime
408+ // while we only keep it for the duration of the call.
404409 val originalEventListener =
405- originalEventListenerMap[call] ? : originalEventListenerCreator?.invoke(call) ? : return
406- originalEventListener.canceled(call)
410+ originalEventListenerMap[call]
411+ ? : fixedOriginalEventListener
412+ // The call already reached its terminal event, so its listener is gone. Call.cancel() is
413+ // documented as a no-op for a completed request, thus there is nothing to report, and
414+ // creating a second listener here would break the Factory contract and leak the entry.
415+ ? : if (call.isExecuted()) null else getOrCreateEventListener(call)
416+ originalEventListener?.canceled(call)
407417 }
408418
409419 override fun satisfactionFailure (call : Call , response : Response ) {
@@ -422,6 +432,13 @@ public open class SentryOkHttpEventListener(
422432 originalEventListenerMap[call]?.cacheConditionalHit(call, cachedResponse)
423433 }
424434
435+ // computeIfAbsent, so that a cancel racing callStart() cannot make the Factory produce two
436+ // listeners for the same Call
437+ private fun getOrCreateEventListener (call : Call ): EventListener ? {
438+ val creator = originalEventListenerCreator ? : return null
439+ return originalEventListenerMap.computeIfAbsent(call) { creator.invoke(it) }
440+ }
441+
425442 private fun canCreateEventSpan (originalEventListener : EventListener ? ): Boolean {
426443 // If the wrapped EventListener is ours, we shouldn't create spans, as the originalEventListener
427444 // already did it
0 commit comments