-
-
Notifications
You must be signed in to change notification settings - Fork 144
feat(worker): resourceLimits for worker isolates #2042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
edusperoni
merged 2 commits into
feat/worker-android-options
from
feat/worker-resource-limits
Sep 11, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| v8-14.9.207.39-6 | ||
| v8-14.9.207.39-7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
test-app/app/src/main/assets/app/tests/testWorkerResourceLimits.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| describe("Worker resourceLimits", function () { | ||
| var echoEntry = "./workerResourceLimitsEchoWorker.js"; | ||
| var oomEntry = "./workerResourceLimitsOomWorker.js"; | ||
|
|
||
| // Jasmine arms a spec's async timeout before calling it, so the interval | ||
| // has to be raised ahead of the spec, not inside it. | ||
| var originalTimeout; | ||
| beforeEach(function () { | ||
| originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL; | ||
| jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000; | ||
| }); | ||
| afterEach(function () { | ||
| jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout; | ||
| }); | ||
|
|
||
| var expectStarts = function (options, done) { | ||
| var worker = options === undefined ? new Worker(echoEntry) : new Worker(echoEntry, options); | ||
| var settled = false; | ||
| var finish = function () { | ||
| if (settled) { | ||
| return; | ||
| } | ||
| settled = true; | ||
| worker.terminate(); | ||
| done(); | ||
| }; | ||
| worker.onmessage = function (msg) { | ||
| expect(msg.data.started).toBe(true); | ||
| finish(); | ||
| }; | ||
| worker.onerror = function (e) { | ||
| expect(String(e && e.message ? e.message : e)).toBe("<no worker error>"); | ||
| finish(); | ||
| }; | ||
| }; | ||
|
|
||
| it("starts a worker under a maxYoungGenerationSizeMb cap", function (done) { | ||
| expectStarts({ resourceLimits: { maxYoungGenerationSizeMb: 8 } }, done); | ||
| }); | ||
|
|
||
| it("starts a worker under both heap caps", function (done) { | ||
| expectStarts({ resourceLimits: { maxOldGenerationSizeMb: 64, maxYoungGenerationSizeMb: 8 } }, | ||
| done); | ||
| }); | ||
|
|
||
| it("treats resourceLimits: null like an absent resourceLimits", function (done) { | ||
| expectStarts({ resourceLimits: null }, done); | ||
| }); | ||
|
|
||
| it("ignores unknown keys inside resourceLimits", function (done) { | ||
| expectStarts({ resourceLimits: { maxOldGenerationSizeMb: 64, somethingElse: 42 } }, done); | ||
| }); | ||
|
|
||
| it("reports a worker that runs out of heap through onerror", function (done) { | ||
| var worker = new Worker(oomEntry, { resourceLimits: { maxOldGenerationSizeMb: 32 } }); | ||
| var settled = false; | ||
|
|
||
| // The entry never returns, so nothing can post: a message here means the | ||
| // cap was not applied at all. | ||
| worker.onmessage = function () { | ||
| expect("worker posted a message").toBe("worker exhausted its heap"); | ||
| }; | ||
|
|
||
| worker.onerror = function (e) { | ||
| if (settled) { | ||
| return; | ||
| } | ||
| settled = true; | ||
| var message = String(e && e.message ? e.message : e); | ||
| expect(message).toMatch(/out of memory/i); | ||
| // Naming the cap is what tells this apart from any other failure | ||
| // the worker could have reported. | ||
| expect(message).toMatch(/maxOldGenerationSizeMb: 32/); | ||
| worker.terminate(); | ||
| done(); | ||
| }; | ||
| }); | ||
|
|
||
| it("throws a TypeError when resourceLimits is not an object", function () { | ||
| expect(function () { | ||
| new Worker(echoEntry, { resourceLimits: 5 }); | ||
| }).toThrowError(TypeError, /"resourceLimits"/); | ||
| }); | ||
|
|
||
| it("throws a TypeError for a non-numeric maxOldGenerationSizeMb", function () { | ||
| expect(function () { | ||
| new Worker(echoEntry, { resourceLimits: { maxOldGenerationSizeMb: "64" } }); | ||
| }).toThrowError(TypeError, /"resourceLimits\.maxOldGenerationSizeMb"/); | ||
| }); | ||
|
|
||
| it("throws a RangeError for a maxOldGenerationSizeMb of zero", function () { | ||
| expect(function () { | ||
| new Worker(echoEntry, { resourceLimits: { maxOldGenerationSizeMb: 0 } }); | ||
| }).toThrowError(RangeError, /"resourceLimits\.maxOldGenerationSizeMb"/); | ||
| }); | ||
|
|
||
| it("throws a RangeError for a NaN maxYoungGenerationSizeMb", function () { | ||
| expect(function () { | ||
| new Worker(echoEntry, { resourceLimits: { maxYoungGenerationSizeMb: NaN } }); | ||
| }).toThrowError(RangeError, /"resourceLimits\.maxYoungGenerationSizeMb"/); | ||
| }); | ||
|
|
||
| // The cap is derived from size_t, so it differs per ABI: 4095 MB where | ||
| // size_t is 32 bits (armeabi-v7a, x86), (2^44 - 1) MB where it is 64. The | ||
| // value has to sit above both for the spec to mean anything on every ABI. | ||
| it("throws a RangeError for a maxOldGenerationSizeMb too large to hold in bytes", function () { | ||
| expect(function () { | ||
| new Worker(echoEntry, { resourceLimits: { maxOldGenerationSizeMb: Math.pow(2, 53) } }); | ||
| }).toThrowError(RangeError, /"resourceLimits\.maxOldGenerationSizeMb"/); | ||
| }); | ||
|
|
||
| it("throws a RangeError for a maxYoungGenerationSizeMb below one byte", function () { | ||
| expect(function () { | ||
| new Worker(echoEntry, { resourceLimits: { maxYoungGenerationSizeMb: 1e-9 } }); | ||
| }).toThrowError(RangeError, /"resourceLimits\.maxYoungGenerationSizeMb"/); | ||
| }); | ||
|
|
||
| it("propagates the error thrown by a resourceLimits getter", function () { | ||
| var boom = new Error("boom"); | ||
| var options = { resourceLimits: new Proxy({}, { | ||
| get: function (target, key) { | ||
| if (key === "maxOldGenerationSizeMb") { | ||
| throw boom; | ||
| } | ||
| return undefined; | ||
| } | ||
| }) }; | ||
| var thrown; | ||
| try { | ||
| new Worker(echoEntry, options); | ||
| } catch (e) { | ||
| thrown = e; | ||
| } | ||
| expect(thrown).toBe(boom); | ||
| }); | ||
|
|
||
| it("throws a RangeError for a jsDispatchTableSizeMb above the ceiling", function () { | ||
| expect(function () { | ||
| new Worker(echoEntry, { resourceLimits: { jsDispatchTableSizeMb: 300 } }); | ||
| }).toThrowError(RangeError, /"resourceLimits\.jsDispatchTableSizeMb"/); | ||
| }); | ||
|
|
||
| it("throws a RangeError for a fractional jsDispatchTableSizeMb", function () { | ||
| expect(function () { | ||
| new Worker(echoEntry, { resourceLimits: { jsDispatchTableSizeMb: 1.5 } }); | ||
| }).toThrowError(RangeError, /"resourceLimits\.jsDispatchTableSizeMb"/); | ||
| }); | ||
|
|
||
| it("starts a worker under a jsDispatchTableSizeMb reservation", function (done) { | ||
| expectStarts({ resourceLimits: { jsDispatchTableSizeMb: 64 } }, done); | ||
| }); | ||
|
|
||
| it("starts a worker under the smallest jsDispatchTableSizeMb reservation", function (done) { | ||
| expectStarts({ resourceLimits: { jsDispatchTableSizeMb: 1 } }, done); | ||
| }); | ||
| }); | ||
3 changes: 3 additions & 0 deletions
3
test-app/app/src/main/assets/app/tests/workerResourceLimitsEchoWorker.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Entry for testWorkerResourceLimits: reports that the isolate came up under | ||
| // whatever resourceLimits the parent passed. | ||
| postMessage({ started: true }); |
6 changes: 6 additions & 0 deletions
6
test-app/app/src/main/assets/app/tests/workerResourceLimitsOomWorker.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Entry for testWorkerResourceLimits: allocates and never releases, so the | ||
| // isolate walks into the maxOldGenerationSizeMb cap the parent set. | ||
| var keep = []; | ||
| for (;;) { | ||
| keep.push(new Array(100000).fill(1)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: NativeScript/android
Length of output: 9596
🤖 get_repo_knowledge executed:
get_repo_knowledge NativeScript/android /tmp/coderabbit-repo-knowledge/nativescript-android-462ff740/learningsLength of output: 748
🏁 Script executed:
Repository: NativeScript/android
Length of output: 19763
🏁 Script executed:
Repository: NativeScript/android
Length of output: 7426
Handle V8 builds without dispatch-table reservation support.
When
V8_HAS_JS_DISPATCH_TABLE_RESERVATION_PARAMis absent,ParseWorkerResourceLimitsthrows anErrorfor validjsDispatchTableSizeMbvalues before worker startup. Gate both startup tests on this capability, or assert the documented error for unsupported builds.🤖 Prompt for AI Agents