Avoid concurrent modification of persistent frame callbacks (#131677)

Fixes https://github.com/flutter/flutter/issues/131415

We should do an audit of all such cases though, filed https://github.com/flutter/flutter/issues/131678
This commit is contained in:
Dan Field 2023-08-01 11:38:40 -07:00 committed by GitHub
parent b9c3f1f74c
commit c57169835a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -1227,7 +1227,7 @@ mixin SchedulerBinding on BindingBase {
try {
// PERSISTENT FRAME CALLBACKS
_schedulerPhase = SchedulerPhase.persistentCallbacks;
for (final FrameCallback callback in _persistentCallbacks) {
for (final FrameCallback callback in List<FrameCallback>.of(_persistentCallbacks)) {
_invokeFrameCallback(callback, _currentFrameTimeStamp!);
}

View File

@ -28,4 +28,21 @@ void main() {
);
timeDilation = 1.0;
});
test('Adding a persistent frame callback during a persistent frame callback', () {
bool calledBack = false;
SchedulerBinding.instance.addPersistentFrameCallback((Duration timeStamp) {
if (!calledBack) {
SchedulerBinding.instance.addPersistentFrameCallback((Duration timeStamp) {
calledBack = true;
});
}
});
SchedulerBinding.instance.handleBeginFrame(null);
SchedulerBinding.instance.handleDrawFrame();
expect(calledBack, false);
SchedulerBinding.instance.handleBeginFrame(null);
SchedulerBinding.instance.handleDrawFrame();
expect(calledBack, true);
});
}