azure-sdk-for-java: OpenAIClient causes "channel not registered to an event loop" error when receiving a 429 response status

Describe the bug When using the OpenAIClient, if I call an endpoint which returns a 429 response with a body, I get an exception with error message channel not registered to an event loop.

Exception or Stack Trace java.lang.IllegalStateException: channel not registered to an event loop

at io.netty.channel.AbstractChannel.eventLoop(AbstractChannel.java:163)
at com.azure.core.http.netty.implementation.NettyUtility.closeConnection(NettyUtility.java:79)
at com.azure.core.http.netty.implementation.NettyAsyncHttpResponse.close(NettyAsyncHttpResponse.java:116)
at com.azure.core.http.policy.RetryPolicy.attemptSync(RetryPolicy.java:249)
at com.azure.core.http.policy.RetryPolicy.processSync(RetryPolicy.java:161)
at com.azure.core.http.HttpPipelineNextSyncPolicy.processSync(HttpPipelineNextSyncPolicy.java:53)
at com.azure.core.http.policy.AddHeadersFromContextPolicy.processSync(AddHeadersFromContextPolicy.java:67)
at com.azure.core.http.HttpPipelineNextSyncPolicy.processSync(HttpPipelineNextSyncPolicy.java:53)
at com.azure.core.http.policy.RequestIdPolicy.processSync(RequestIdPolicy.java:77)
at com.azure.core.http.HttpPipelineNextSyncPolicy.processSync(HttpPipelineNextSyncPolicy.java:53)
at com.azure.core.http.policy.HttpPipelineSyncPolicy.processSync(HttpPipelineSyncPolicy.java:51)
at com.azure.core.http.policy.UserAgentPolicy.processSync(UserAgentPolicy.java:174)
at com.azure.core.http.HttpPipelineNextSyncPolicy.processSync(HttpPipelineNextSyncPolicy.java:53)
at com.azure.core.http.HttpPipeline.sendSync(HttpPipeline.java:138)
at com.azure.core.implementation.http.rest.SyncRestProxy.send(SyncRestProxy.java:62)
at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:83)
at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:124)
at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:95)
at jdk.proxy2/jdk.proxy2.$Proxy10.getChatCompletionsSync(Unknown Source)
at com.azure.ai.openai.implementation.OpenAIClientImpl.getChatCompletionsWithResponse(OpenAIClientImpl.java:1688)
at com.azure.ai.openai.OpenAIClient.getChatCompletionsWithResponse(OpenAIClient.java:329)
at com.azure.ai.openai.OpenAIClient.getChatCompletions(OpenAIClient.java:696)

To Reproduce Use the code below against an endpoint which returns 429

Code Snippet

        OpenAIClientBuilder openAIClientBuilder = new OpenAIClientBuilder()
                .endpoint(<ENDOINT>)
                .credential(new KeyCredential(<KEY>))
                .serviceVersion(OpenAIServiceVersion.V2024_06_01);

        String tokens = IntStream.range(0, 10000)
                .mapToObj(Integer::toString)
                .collect(Collectors.joining(" "));

        OpenAIClient openAIClient = openAIClientBuilder.buildClient();
        ChatCompletionsOptions chatCompletionsOptions = new ChatCompletionsOptions(List.of(
                new ChatRequestSystemMessage("Count the tokens")
                , new ChatRequestUserMessage(tokens)));
       openAIClient.getChatCompletions(<DEPLOYMENT>, chatCompletionsOptions);

Expected behavior The client to throw a suitable exception

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 19 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Hi, I have created a simple project that reproduces this issue https://github.com/joanna-kjm/azure-sdk-issue-43583

It’s not related to the duration passed by azure in “Retry-After” header - it doesn’t matter how how long client stops. As @robmartin-scibite said it’s something inside retry mechanizm and I believe it only happens with netty client (default one). For me the solution is to use okhttp instead.

There are both netty and okhttp clients tests in my sample. I was only able to reproduce the issue when netty client is in use.

I think the above is a bug in com.azure.core.http.policy.RetryPolicy which on line 249 calls httpResponse.close() which attempts to close the response without reading the response body. If I use the following retry policy everything works corrrectly:

ExponentialBackoffOptions exponentialBackoffOptions = new ExponentialBackoffOptions();
exponentialBackoffOptions.setMaxRetries(0);
RetryOptions retryOptions = new RetryOptions(exponentialBackoffOptions);

openAIClientBuilder.retryOptions(retryOptions);