question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bail should prevent any further calls to the retrier function

See original GitHub issue

From the example:

// Packages
const retry = require('async-retry')
const fetch = require('node-fetch')
 
await retry(async bail => {
  // if anything throws, we retry
  const res = await fetch('https://google.com')
 
  if (403 === res.status) {
    // don't retry upon 403
    bail(new Error('Unauthorized'))
    // return  <---- don't immediately return here
    throw new Error('Throw after bail'); 
  }
 
  const data = await res.text()
  return data.substr(0, 500)
}, {
  retries: 5
})

Calling bail will immediately reject the promise returned by retry, but if the promise returned by the retirer function is then itself rejected, attempts will continue running in the background until e.g. the max retry count is reached.

This can be surprising and result in obscure bugs.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:13
  • Comments:14

github_iconTop GitHub Comments

5reactions
przemyslaw-wlodekcommented, Apr 9, 2021

I’ve faced the same issue and for me it feels like an unexpected behaviour. Unfortunately it was not easy to catch until I’ve seen application logs that appeared far after the promise execution (bail).

I think that retries should be simply stopped after bail was called.

Pattern provided in README example breaks TypeScript typings:

  if (403 === res.status) {
    // don't retry upon 403
    bail(new Error('Unauthorized'))
    return
  }

which leads to an awful type-casting e.g.

  if (403 === res.status) {
    // don't retry upon 403
    bail(new Error('Unauthorized'))
    return (null as unknown) as PromiseReturnType;
  }
1reaction
agalatancommented, Nov 28, 2022

Is this actually still an issue? I wrote these Jest tests that pass, which suggest it works just fine:

// @flow strict
import retry from 'async-retry';

describe('async-retry', () => {
  const bailError = new Error('BAIL');
  const rejectError = new Error('BOOM');

  test('retries N times', async () => {
    const cb = jest.fn().mockImplementation(
      async () => {
        throw rejectError;
      }
    );

    await expect(
      retry(cb, { retries: 3, factor: 1, minTimeout: 0 })
    ).rejects.toBe(rejectError);

    expect(cb).toHaveBeenCalledTimes(4);
  });

  test('does not keep retrying once bail out is called', async () => {
    const cb = jest.fn().mockImplementation(async (bail, count) => {
      console.log('START');

      if (count === 2) {
        bail(bailError);
        console.log('BAIL');
      }
      throw rejectError;
    });

    await expect(
      retry(cb, { retries: 5, factor: 1, minTimeout: 0 })
    ).rejects.toBe(bailError);

    expect(cb).toHaveBeenCalledTimes(2);
  });
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

bail should prevent any further calls to the retrier function #69
Calling bail will immediately reject the promise returned by retry , but if the promise returned by the retirer function is then itself...
Read more >
Why We Can't Go Backwards on Bail Reform
In 2019, the New York State Legislature passed a bail reform law that pushed New York's criminal legal system in the right direction....
Read more >
Bail - How Courts Work
The purpose of bail is simply to ensure that defendants will appear for trial and all pretrial hearings for which they must be...
Read more >
A Brief on Bail Practices - Office of Justice Programs
--Any person denied bailor release, should be entitled to review of his case after 72 hours of detention, and if. detentiDn is continued...
Read more >
dog trainers for aggressive dogs
Aggressive behavior in dogs means any behavior preceding or including an attack. ... After a few weeks, your dog should stop attacking furniture...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found