Skip to content

Commit 7b7f12e

Browse files
committed
fix(okhttp): drop out-of-window cancels instead of creating a listener for them
1 parent 728129d commit 7b7f12e

2 files changed

Lines changed: 38 additions & 31 deletions

File tree

‎sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpEventListener.kt‎

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,25 @@ public open class SentryOkHttpEventListener(
403403
}
404404

405405
override fun canceled(call: Call) {
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.
409-
val originalEventListener =
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)
406+
// canceled() is the only callback that is not part of the call window. OkHttp creates the
407+
// listener in the Call constructor and keeps it for the whole lifetime of the Call, so it may
408+
// deliver canceled() before callStart() and after callEnd()/callFailed(). We only keep the
409+
// listener between callStart() and the terminal event, so out of that window there is nothing
410+
// in originalEventListenerMap.
411+
//
412+
// We deliberately do not create a listener for such a cancel:
413+
// - After the terminal event the call is over. Call.cancel() is documented as a no-op for a
414+
// request that is already complete, so there is nothing to report.
415+
// - Before callStart() there may never be a call at all. A Call that is canceled and then
416+
// never executed gets no callEnd()/callFailed(), so an entry added here could never be
417+
// removed again and would leak the Call. If the Call is executed after all, OkHttp fails it
418+
// with IOException("Canceled") and the listener still learns about it through callFailed().
419+
//
420+
// Creating one would also hand the EventListener.Factory contract a second listener for a
421+
// single Call. A listener kept in a field instead is shared by all calls by definition, the
422+
// same as OkHttp's own EventListener.asFactory(), so it exists outside the window and always
423+
// gets the event.
424+
val originalEventListener = originalEventListenerMap[call] ?: fixedOriginalEventListener
416425
originalEventListener?.canceled(call)
417426
}
418427

@@ -432,8 +441,8 @@ public open class SentryOkHttpEventListener(
432441
originalEventListenerMap[call]?.cacheConditionalHit(call, cachedResponse)
433442
}
434443

435-
// computeIfAbsent, so that a cancel racing callStart() cannot make the Factory produce two
436-
// listeners for the same Call
444+
// computeIfAbsent, so that concurrent callbacks cannot make the Factory produce two listeners
445+
// for the same Call
437446
private fun getOrCreateEventListener(call: Call): EventListener? {
438447
val creator = originalEventListenerCreator ?: return null
439448
return originalEventListenerMap.computeIfAbsent(call) { creator.invoke(it) }

‎sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpEventListenerDelegationTest.kt‎

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,6 @@ class SentryOkHttpEventListenerDelegationTest {
5454

5555
fun newCall(path: String): Call =
5656
client.newCall(Request.Builder().url("http://localhost/$path").build())
57-
58-
// The tests drive the listener by hand, so no call is ever really executed. canceled() branches
59-
// on Call.isExecuted() to tell a cancel that precedes callStart() from one that follows the
60-
// terminal event, so those cases need a Call that reports the state under test.
61-
fun mockCall(path: String, isExecuted: Boolean): Call {
62-
val request = Request.Builder().url("http://localhost/$path").build()
63-
return mock<Call>().also {
64-
whenever(it.request()).thenReturn(request)
65-
whenever(it.isExecuted()).thenReturn(isExecuted)
66-
}
67-
}
6857
}
6958

7059
private val fixture = Fixture()
@@ -150,25 +139,34 @@ class SentryOkHttpEventListenerDelegationTest {
150139
}
151140

152141
@Test
153-
fun `cancel before callStart binds the listener that callStart then reuses`() {
142+
fun `cancel before callStart is not delegated and creates no listener`() {
154143
val sut = fixture.getSut()
155-
val call = fixture.mockCall("1", isExecuted = false)
144+
val call = fixture.newCall("1")
145+
146+
sut.canceled(call)
147+
148+
assertThat(fixture.listeners).isEmpty()
149+
}
150+
151+
@Test
152+
fun `a call canceled before callStart still gets a single listener when it starts`() {
153+
val sut = fixture.getSut()
154+
val call = fixture.newCall("1")
156155

157156
sut.canceled(call)
158157
sut.callStart(call)
159-
sut.dnsStart(call, "sentry.io")
160-
sut.callEnd(call)
158+
sut.callFailed(call, IOException("Canceled"))
161159

162160
assertThat(fixture.listeners).hasSize(1)
163161
assertThat(fixture.listeners.single().received.map { it.first })
164-
.containsExactly("canceled", "callStart", "dnsStart", "callEnd")
162+
.containsExactly("callStart", "callFailed")
165163
.inOrder()
166164
}
167165

168166
@Test
169167
fun `cancel after the terminal event is ignored`() {
170168
val sut = fixture.getSut()
171-
val call = fixture.mockCall("1", isExecuted = true)
169+
val call = fixture.newCall("1")
172170

173171
sut.callStart(call)
174172
sut.callEnd(call)
@@ -183,7 +181,7 @@ class SentryOkHttpEventListenerDelegationTest {
183181
@Test
184182
fun `cancel after a failed call is ignored`() {
185183
val sut = fixture.getSut()
186-
val call = fixture.mockCall("1", isExecuted = true)
184+
val call = fixture.newCall("1")
187185

188186
sut.callStart(call)
189187
sut.callFailed(call, IOException())
@@ -198,7 +196,7 @@ class SentryOkHttpEventListenerDelegationTest {
198196
@Test
199197
fun `a single wrapped listener receives cancels outside of the call window`() {
200198
whenever(fixture.scopes.options).thenReturn(SentryOptions())
201-
val call = fixture.mockCall("1", isExecuted = true)
199+
val call = fixture.newCall("1")
202200
val listener = RecordingListener(call)
203201
val sut = SentryOkHttpEventListener(fixture.scopes, listener)
204202

0 commit comments

Comments
 (0)