Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to close OkHttpClient when using DefaultOkHttpOpenAiClient #25

Open
mscheong01 opened this issue Jun 11, 2023 · 2 comments · May be fixed by #26
Open

Provide a way to close OkHttpClient when using DefaultOkHttpOpenAiClient #25

mscheong01 opened this issue Jun 11, 2023 · 2 comments · May be fixed by #26

Comments

@mscheong01
Copy link
Owner

When executing a Test where an error occurs in InvocationHandler, it takes a while for the system to exit after the exception is thrown. It looks like the probable cause is that the background thread that calls the openai api is not cleaned up and runs regardless of the error. This did not happen when I used OpenAiApiClientdefined in the spring boot starter that uses a spring webclient.
suspicious code:
https://github.com/mscheong01/interfAIce/blob/main/core/src/main/kotlin/io/github/mscheong01/interfaice/openai/DefaultOkHttpOpenAiClient.kt

Mono.create { sink ->
    client.newCall(httpRequest).enqueue(object : com.squareup.okhttp.Callback {
        override fun onFailure(request: Request, e: java.io.IOException) {
            sink.error(e)
        }

        override fun onResponse(response: com.squareup.okhttp.Response) {
            try {
                if (!response.isSuccessful) {
                    throw IOException("Error: ${response.code()} ${response.message()}")
                } else {
                    sink.success(mapper.readValue(response.body().string()))
                }
            } catch (e: Exception) {
                sink.error(e)
            }
        }
    })
}
@mscheong01 mscheong01 changed the title Background threads are not cleaned up when using DefaultOkHttpOpenAiClient Cannot close OkHttpClient when using DefaultOkHttpOpenAiClient Jun 11, 2023
@mscheong01
Copy link
Owner Author

mscheong01 commented Jun 11, 2023

The cause turned out to be that the OkHttpClient used in DefaultOkHttpOpenAiClient is not closed, leaving connection threads that keep the application from exiting. We could make the OpenAiProxyFactory and OpenAiApiAdapter implement Closable. However, this would mean that closing an OpenAiProxyFactory would stop all proxies created from it from working. I'm not sure if this is an acceptable pattern 🤔

@mscheong01 mscheong01 linked a pull request Jun 11, 2023 that will close this issue
@mscheong01
Copy link
Owner Author

I've implemented the above comment here, but I'll leave it open until I'm sure that it's a good implementation

@mscheong01 mscheong01 changed the title Cannot close OkHttpClient when using DefaultOkHttpOpenAiClient Provide a way to close OkHttpClient when using DefaultOkHttpOpenAiClient Jun 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant