Skip to content

Creating a new Connection causes an infinite refresh. #1766

Open

Description

@Matthew-Zvantsov

Hello, I have a problem. I am using jsforce version 1.11 on my project. Now I decided to upgrade to version 3 and encountered an error.
My code creates a new connection, passing in a perfectly valid refreshToken and instanceUrl. The passed instanceUrl is valid, but not the one returned by Salesforce.
For example, I have an instanceUrl:
https://mdc-hause.my.salesforce.com
JsForce V1 and Salesforce CLI work with this URL.
I get the following URL when creating a connection:
https://someKey-playground1.my.salesforce.com
Note: These URLs are provided as an example to show that they are the same org. Salesforce previously replaced one with the other, so they both work. However, JsForce V3 doesn't work with the older one.
However, when attempting to make a request on this connection, the .on('refresh') event is called indefinitely:

const creds = {
          oauth2: {
            validClientId,
            validClientSecret,
          },
          refreshToken: validRefreshToken,
          instanceUrl: validProperInstanceUrl,
          version: '60.0',
        };

connection = new jsforce.Connection(creds);

connection.on('refresh', (accessToken) => {
  console.log('instanceUrl', connection.instanceUrl); 
 // Here, the connection already has an refreshed instanceUrl. This log is reproduced endlessly.
});

const q = connection.query(someQuery)
      .on('record', (record) => {
        records.push(record);
      })
      .on('end', () => {
        resolve(records);
      })
      .on('error', (error) => {
        reject(error);
      });

console.log('Before query run');

q.run(JSFORCE_OPTIONS)
      .catch((err) => {
        reject(err);
      });

However, when I try to execute the request, the 'refresh' event occurs again and again, and the request fails. In the terminal, I see the following:

Before query run
instanceUrl https://someKey-playground1.my.salesforce.com
instanceUrl https://someKey-playground1.my.salesforce.com
instanceUrl https://someKey-playground1.my.salesforce.com
instanceUrl https://someKey-playground1.my.salesforce.com
instanceUrl https://someKey-playground1.my.salesforce.com
......

Note: The above code works for jsforce V1.

Activity

  1. cristiand391 commented on Oct 20, 2025

    @cristiand391
    Contributor

    hey 👋🏼

    a few questions:

    in this block:

    const creds = {
              oauth2: {
                validClientId,
                validClientSecret,
              },
              refreshToken: validRefreshToken,
              instanceUrl: validProperInstanceUrl,
              version: '60.0',
            };
    
    1. is validProperInstanceUrl the old my domain URL or the latest one?
    2. if the using the old one, do you still get into the infinite refresh if starting the code with the new URL?
  2. Matthew-Zvantsov commented on Oct 21, 2025

    @Matthew-Zvantsov
    Author

    hey 👋🏼

    a few questions:

    in this block:

    const creds = {
              oauth2: {
                validClientId,
                validClientSecret,
              },
              refreshToken: validRefreshToken,
              instanceUrl: validProperInstanceUrl,
              version: '60.0',
            };
    
    1. is validProperInstanceUrl the old my domain URL or the latest one?
    2. if the using the old one, do you still get into the infinite refresh if starting the code with the new URL?

    Hi!

    1. Yes. It is old my domain URL.
    2. I get infinite refresh when I use OLD instanceUrl in jsforce 3. If I use the latest instance Url the problem goes away. However, jsforce 1 works equally well in both cases.
  3. Matthew-Zvantsov commented on Nov 3, 2025

    @Matthew-Zvantsov
    Author

    @cristiand391 I wanted to check in to see if anyone had a chance to look at my question above. Thanks in advance!

  4. amitbeck commented on Feb 5, 2026

    @amitbeck

    In my case the issue was that I was using OAuth2 credentials for the wrong instance (production instance URL instead of sandbox instance URL).

    Kinda surprised it was stuck in a refresh event loop instead of the connection APIs rejecting...

  5. cristiand391 commented on Feb 5, 2026

    @cristiand391
    Contributor

    @amitbeck the request was probably returning the status code/response that triggers a session-refresh.
    If you can reproduce it with this env var set JSFORCE_LOG_LEVEL=DEBUG and share the request details when it gets stuck I can look into it (you can redact org instances URLs, we only care about the response status code)

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions