airqo-platform / airqo-platform/AirQo-api

Fix potential race condition in message consumer timeout handling

未关闭
#4,717 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
JavaScript
星标
26
派生
24
平均合并
5 小时 36 分钟
30 天内合并 PR
81

描述

## Issue Description

The current implementation of the message consumer timeout handling in `src/auth-service/bin/start-consumer.js` has a potential issue where the timeout callback is never cleared if the consumer initialization completes after the race has been won by the timeout promise.

This can lead to misleading 'timed out' warnings in the logs and a small memory leak.

## Suggested Fix

Add a `clearTimeout` call after the race is resolved to cancel the timeout callback:

```diff
- const consumerPromise = messageConsumer();
- const timeoutPromise = new Promise((resolve) => {
- const timeoutMs = constants.MESSAGE_CONSUMER_STARTUP_TIMEOUT_MS || 10000;
- setTimeout(() => {
+ const consumerPromise = messageConsumer();
+ let timeoutId;
+ const timeoutPromise = new Promise((resolve) => {
+ const timeoutMs = constants.MESSAGE_CONSUMER_STARTUP_TIMEOUT_MS || 10000;
+ timeoutId = setTimeout(() => {
...
- }, timeoutMs);
+ }, timeoutMs);
});

- const result = await Promise.race([consumerPromise, timeoutPromise]);
+ const result = await Promise.race([consumerPromise, timeoutPromise]);
+ clearTimeout(timeoutId);
```

## References
- PR: https://github.com/airqo-platform/AirQo-api/pull/4716
- Comment: https://github.com/airqo-platform/AirQo-api/pull/4716#discussion_r2051508777

This issue was identified by CodeRabbit during code review.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。