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

Memory leak with AsyncRabbitTemplate #2673

Closed
tetrade opened this issue Apr 3, 2024 · 5 comments
Closed

Memory leak with AsyncRabbitTemplate #2673

tetrade opened this issue Apr 3, 2024 · 5 comments

Comments

@tetrade
Copy link

tetrade commented Apr 3, 2024

3.0.10

When i use org.springframework.amqp.rabbit.AsyncRabbitTemplate#sendAndReceive(java.lang.String, org.springframework.amqp.core.Message) and when get reply, nothing is clearing or canceling org.springframework.amqp.rabbit.RabbitFuture#timeoutTask and for every sending (if i get answer) i got memory leak , as shown in the photo (find this every time with profiler)

image

I got bug with this snippet of code:

                Message message = MessageBuilder
                        .withBody(mapper.writeValueAsBytes(photoMapper.mapToCkSendProcessDto(imageModel)))
                        .setContentType(MessageProperties.CONTENT_TYPE_JSON).build();

                log.debug("Send image {} to process", imageModel.getId());
                return asyncRabbitTemplate.sendAndReceive(queue.getActualName(), message)
                        .thenApply(mes -> {
                            log.debug("Get image {} after process", imageModel.getId());
                            try {
                             // ..........
                            } catch (IOException e) {
                                throw new RuntimeException(e);
                            }
                        }).handle((el, err) -> err == null ? el : null);
    @Bean
    public ConnectionFactory rabbitConnectionFactory(com.rabbitmq.client.ConnectionFactory cf) {
        return new CachingConnectionFactory(cf);
    }

    @Bean
    public RabbitTemplate template(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        return rabbitTemplate;
    }

    @Bean
    public AsyncRabbitTemplate asyncRabbitTemplate(RabbitTemplate template) {
        AsyncRabbitTemplate asyncRabbitTemplate = new AsyncRabbitTemplate(template);
        asyncRabbitTemplate.setReceiveTimeout(6000000);
        return asyncRabbitTemplate;
    }
@artembilan
Copy link
Member

I’m on sick day off, so if you have an idea how to fix, feel free to contribute it then! I’ll be happy to review when I’m back.
Thanks

@tetrade
Copy link
Author

tetrade commented Apr 4, 2024

I’m on sick day off, so if you have an idea how to fix, feel free to contribute it then! I’ll be happy to review when I’m back.
Thanks

I wish you a speedy recovery ! Okay I will try to fix it :)

@artembilan
Copy link
Member

Hi @tetrade !

I'm back from leave and ready to tackle this.
Let me know if you have started looking into this, so we don't cross each other.
We have a release next Monday if that matters somehow...

Thanks

@artembilan artembilan added this to the 3.1.4 milestone Apr 8, 2024
spring-builds pushed a commit that referenced this issue Apr 8, 2024
Fixes: #2673

When `AsyncRabbitTemplate#sendAndReceive()` is called a got reply,
nothing is clearing or canceling a `RabbitFuture.timeoutTask`

* Fix `RabbitFuture` overriding `complete(T value)` and `completeExceptionally(Throwable ex)`
and call `this.timeoutTask.cancel(true)`

(cherry picked from commit 50a06fc)
@artembilan
Copy link
Member

So, apparently the test and fix are simple, so I decided to go ahead and do it myself.

Feel free to contribute anything else!

@tetrade
Copy link
Author

tetrade commented Apr 8, 2024

Yeah ! I just study and don't see your message :) Next time you will see my contribution 👍
Good resolve of the bug

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

No branches or pull requests

3 participants