Skip to content

fix(logging): ensure callback executes even if Application Insights throws#791

Open
yashkohli88 wants to merge 1 commit into
clearlydefined:masterfrom
yashkohli88:yk/logger-callback-safety
Open

fix(logging): ensure callback executes even if Application Insights throws#791
yashkohli88 wants to merge 1 commit into
clearlydefined:masterfrom
yashkohli88:yk/logger-callback-safety

Conversation

@yashkohli88
Copy link
Copy Markdown
Contributor

Problem

ApplicationInsightsTransport.log() callback could be skipped if Application Insights client throws exception during trackTrace/trackException calls.
This would cause Winston to hang.

Solution

Wrap AI tracking calls in try/finally block to guarantee callback execution regardless of whether tracking succeeds or fails.

Context

Implements feedback from @JamieMagee on clearlydefined/service#1582 - same issue applies to crawler's ApplicationInsightsTransport implementation
added in #739 #740

Changes

  • Moved callback() into finally block in ApplicationInsightsTransport.log()
  • AI tracking logic now wrapped in try block
  • Callback executes even if trackTrace/trackException throws

…hrows

Wrap Application Insights tracking calls in try/finally block to prevent
Winston from hanging if the AI client throws an exception. Without this,
a failed trackTrace or trackException call would skip the callback,
causing Winston to stall.

Addresses feedback from clearlydefined/service#1582
@qtomlinson qtomlinson requested a review from JamieMagee May 26, 2026 16:54
const err = new Error(info.message)
err.stack = info.stack
this.aiClient.trackException({ exception: err, properties })
try {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No catch here, so a throw from the AI client still escapes through Winston into the caller. The callback fires, but the calling code crashes. Catch it and drop the telemetry quietly (or console.error it). Losing a log line beats killing the request that produced it.

severity: mapLevel(info.level),
properties
})
} finally {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a catch paired with this. Without one the exception still propagates after callback() runs, so we get the callback guarantee but lose the safety.

Copy link
Copy Markdown
Contributor

@JamieMagee JamieMagee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finally makes sure the callback runs, but without a catch the exception from trackTrace / trackException still escapes through Winston into whoever called logger.info(). That swaps the silent hang for a crash on every failed telemetry call. Wrapping in try / catch / finally and dropping (or console.error-ing) the error in the catch fixes it.

Worth adding a test too: stub trackTrace to throw and assert that logger.info() doesn't throw and that the callback still fires. The current suite only hits the happy path, which is why this slipped through.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants