Skip to content

Fix potential race condition in message consumer timeout handling #4717

@coderabbitai

Description

@coderabbitai

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:

-    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

This issue was identified by CodeRabbit during code review.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions