@samitouri / QOS-React-2 / commits / 4129ea8c92

set priority on TaskController instead of on postTask/yield (#27295)

## Summary passing both a signal and a priority to `postTask`/`yield` in chrome causes memory to spike and potentially causes OOMs. a fix for this has landed in chrome 118, but we can avoid the issue in earlier versions by setting priority on just the TaskController instead. https://bugs.chromium.org/p/chromium/issues/detail?id=1469367 ## How did you test this change? ``` yarn test SchedulerPostTask ```

Noah Lemen committed Aug 29, 2023 at 09:06 UTC 4129ea8c922b950be3964f98d2bb74ff4a1c5431
2 files changed +11 -9
packages/scheduler/src/__tests__/SchedulerPostTask-test.js
+6 -4
@@ -83,7 +83,8 @@ describe('SchedulerPostTask', () => {
83 const scheduler = {};
84 global.scheduler = scheduler;
85
86 - scheduler.postTask = function (callback, {priority, signal}) {
86 + scheduler.postTask = function (callback, {signal}) {
87 + const {priority} = signal;
88 const id = idCounter++;
89 log(
90 `Post Task ${id} [${priority === undefined ? '<default>' : priority}]`,
@@ -94,7 +95,8 @@ describe('SchedulerPostTask', () => {
95 });
96 };
97
97 - scheduler.yield = function ({priority, signal}) {
98 + scheduler.yield = function ({signal}) {
99 + const {priority} = signal;
100 const id = idCounter++;
101 log(`Yield ${id} [${priority === undefined ? '<default>' : priority}]`);
102 const controller = signal._controller;
@@ -111,8 +113,8 @@ describe('SchedulerPostTask', () => {
113 };
114
115 global.TaskController = class TaskController {
114 - constructor() {
115 - this.signal = {_controller: this};
116 + constructor({priority}) {
117 + this.signal = {_controller: this, priority};
118 }
119 abort() {
120 const task = taskQueue.get(this);
packages/scheduler/src/forks/SchedulerPostTask.js
+5 -5
@@ -10,7 +10,7 @@
10 import type {PriorityLevel} from '../SchedulerPriorities';
11
12 declare class TaskController {
13 - constructor(priority?: string): TaskController;
13 + constructor(options?: {priority?: string}): TaskController;
14 signal: mixed;
15 abort(): void;
16 }
@@ -95,9 +95,8 @@ export function unstable_scheduleCallback<T>(
95 break;
96 }
97
98 - const controller = new TaskController();
98 + const controller = new TaskController({priority: postTaskPriority});
99 const postTaskOptions = {
100 - priority: postTaskPriority,
100 delay: typeof options === 'object' && options !== null ? options.delay : 0,
101 signal: controller.signal,
102 };
@@ -130,9 +129,10 @@ function runTask<T>(
129 if (typeof result === 'function') {
130 // Assume this is a continuation
131 const continuation: SchedulerCallback<T> = (result: any);
133 - const continuationController = new TaskController();
134 - const continuationOptions = {
132 + const continuationController = new TaskController({
133 priority: postTaskPriority,
134 + });
135 + const continuationOptions = {
136 signal: continuationController.signal,
137 };
138 // Update the original callback node's controller, since even though we're