Skip to content

fix: resolve Promise.all concurrent query collision with identical SQ… - #2465

Merged
mathiasrw merged 4 commits into
AlaSQL:developfrom
imsanupm:develop
Oct 21, 2026
Merged

mathiasrw merged 4 commits into
AlaSQL:developfrom
imsanupm:develop

Conversation

@imsanupm

@imsanupm imsanupm commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes #1953

When two async functions with identical SQL strings were executed
concurrently via Promise.all, no results were returned and no error
was thrown.

Root Cause

Two separate issues were working together to cause this bug:

  1. cleanupCache mutating shared cached object — The compiled
    statement stored in sqlCache is shared across concurrent executions.
    cleanupCache was clearing statement.query.data = [] on this shared
    object while another concurrent call was still reading it, silently
    wiping the results.

  2. Shared params reference — Concurrent executions via Promise.all
    were sharing the same params reference, allowing one call to overwrite
    another's parameters (f1/f2).

Why it was hard to notice

  • Running functions individually worked fine
  • Running with different SQL strings (different LIMIT values) worked
    fine because they got different cache keys
  • No error was thrown — results were just silently empty
  • Only triggered by concurrent identical SQL strings via Promise.all

Changes

  • cleanupCache() — Removed mutation of statement.query.data on the
    shared cached statement object
  • alasql.dexec() — Clone params before passing to cached statement
    execution to isolate each concurrent call's parameters

How to reproduce (before fix)

async function fun1() {
    return await alasql.promise(`select * from xlsx(?)`, ["D:\\a1.xlsx"]);
}
async function fun2() {
    return await alasql.promise(`select * from xlsx(?)`, ["D:\\a2.xlsx"]);
}

// Returns empty, no error thrown
const results = await Promise.all([fun1(), fun2()]);

Testing

  • Identical SQL strings in Promise.all now return correct results
  • Different SQL strings still work as before
  • Sequential (non-concurrent) queries unaffected
  • Queries without cache (alasql.options.cache = false) unaffected

@mathiasrw

Copy link
Copy Markdown
Member

Ohhh, very nice.

Please make a file named /test/test-1953.js where we demonstrate the issue and will catch if we ever accidently introduce the error again.

@mathiasrw mathiasrw assigned mathiasrw and imsanupm and unassigned mathiasrw Apr 20, 2026

@mathiasrw mathiasrw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please provide a test file named test/test1953.js that fails with the original code and that does not failed with the modified code.

Comment thread src/17alasql.js Resolved

@mathiasrw mathiasrw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We need a test file that fails before the merge and passes after the merge.

@mathiasrw

Copy link
Copy Markdown
Member

@imsanupm are you still on this?

@mathiasrw

Copy link
Copy Markdown
Member

Thank you @imsanupm for opening this and for tracking down where the collision happens. We kept your PR and pushed two commits to it, so it can be merged with you as the author:

  1. Merged develop into the branch (the conflict in src/17alasql.js was resolved to develop's version).
  2. Replaced the change with a guard in alasql.dexec: a cached statement is marked as running until its run has finished (for an asynchronous run, until the callback). A call that arrives while it is still running (a second Promise.all entry, or a nested call from a user function) parses and compiles its own copy instead of sharing it. The cached result data is cleaned up when the run has really finished.

Why the change: keeping query.data made every cached statement hold its last result (test 2007 failed, and memory grew with each distinct query), and cloning the parameters left the rows of the two runs shared.

test/test1953.js covers the report (Promise.all over two files with the same query), two overlapping asynchronous runs, a user function that runs the same query again (with and without GROUP BY), and that no result data stays in the cache. All five fail on develop and pass here; the full suite is green.

Closes #1953.

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.

Promise.all error

2 participants