Skip to content

Commit 247c05a

Browse files
stainless-app[bot]stainless-bot
authored andcommitted
fix(client): async streaming flakiness (#162)
1 parent 7c4a4e0 commit 247c05a

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

openai-java-core/src/main/kotlin/com/openai/core/http/PhantomReachableClosingAsyncStreamResponse.kt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.openai.core.http
22

33
import com.openai.core.closeWhenPhantomReachable
44
import com.openai.core.http.AsyncStreamResponse.Handler
5+
import java.util.Optional
56
import java.util.concurrent.Executor
67

78
internal class PhantomReachableClosingAsyncStreamResponse<T>(
@@ -12,13 +13,30 @@ internal class PhantomReachableClosingAsyncStreamResponse<T>(
1213
}
1314

1415
override fun subscribe(handler: Handler<T>): AsyncStreamResponse<T> = apply {
15-
asyncStreamResponse.subscribe(handler)
16+
asyncStreamResponse.subscribe(HandlerReferencingAsyncStreamResponse(handler, this))
1617
}
1718

1819
override fun subscribe(handler: Handler<T>, executor: Executor): AsyncStreamResponse<T> =
1920
apply {
20-
asyncStreamResponse.subscribe(handler, executor)
21+
asyncStreamResponse.subscribe(
22+
HandlerReferencingAsyncStreamResponse(handler, this),
23+
executor
24+
)
2125
}
2226

2327
override fun close() = asyncStreamResponse.close()
2428
}
29+
30+
/**
31+
* A wrapper around a `Handler` that also references an `AsyncStreamResponse` so that the latter
32+
* will not only be phantom reachable and get reclaimed early while the handler itself is reachable
33+
* and subscribed to the response.
34+
*/
35+
private class HandlerReferencingAsyncStreamResponse<T>(
36+
private val handler: Handler<T>,
37+
private val asyncStreamResponse: AsyncStreamResponse<T>
38+
) : Handler<T> {
39+
override fun onNext(value: T) = handler.onNext(value)
40+
41+
override fun onComplete(error: Optional<Throwable>) = handler.onComplete(error)
42+
}

0 commit comments

Comments
 (0)