Skip to content

Provide a way to close OkHttpClient when using DefaultOkHttpOpenAiClient #25

Description

@mscheong01

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)
            }
        }
    })
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions