From 1ae3dd590680cde13656c9018d5a5eae83aa567b Mon Sep 17 00:00:00 2001 From: Dusty Holmes Date: Fri, 29 Sep 2023 10:26:12 -0500 Subject: [PATCH 1/4] Avoid accessing private members of child modules In dart 2.19 mocks can no longer handle calls into these private members. Perform the call carefully. --- lib/src/lifecycle_module.dart | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/src/lifecycle_module.dart b/lib/src/lifecycle_module.dart index bccd618..3fc413c 100644 --- a/lib/src/lifecycle_module.dart +++ b/lib/src/lifecycle_module.dart @@ -528,7 +528,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { try { manageDisposable(childModule); _childModules.add(childModule); - childModule._parentContext = _loadContext; + _setParentContextOnChild(childModule, _loadContext); await childModule.load(); try { @@ -554,7 +554,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { _didLoadChildModuleController.addError(error, stackTrace); completer.completeError(error, stackTrace); } finally { - childModule._parentContext = null; + _setParentContextOnChild(childModule, null); } }).catchError((Object error, StackTrace stackTrace) { _logger.severe( @@ -569,6 +569,16 @@ abstract class LifecycleModule extends SimpleModule with Disposable { return completer.future; } + /// This method is a workaround for the dangers of mock modules. A mock cannot implement a private member, so this will fail. + void _setParentContextOnChild( + LifecycleModule? childModule, + SpanContext? context, + ) { + try { + childModule?._parentContext = context; + } catch (e) {} + } + /// Public method to suspend the module. /// /// Suspend indicates to the module that it should go into a low-activity @@ -1034,9 +1044,9 @@ abstract class LifecycleModule extends SimpleModule with Disposable { List> childResumeFutures = >[]; for (var child in _childModules.toList()) { childResumeFutures.add(Future.sync(() { - child._parentContext = _activeSpan?.context; + _setParentContextOnChild(child, _activeSpan?.context); return child.resume().whenComplete(() { - child._parentContext = null; + _setParentContextOnChild(child, null); }); })); } @@ -1071,9 +1081,9 @@ abstract class LifecycleModule extends SimpleModule with Disposable { List> childSuspendFutures = >[]; for (var child in _childModules.toList()) { childSuspendFutures.add(Future.sync(() async { - child._parentContext = _activeSpan?.context; + _setParentContextOnChild(child, _activeSpan?.context); return child.suspend().whenComplete(() { - child._parentContext = null; + _setParentContextOnChild(child, null); }); })); } @@ -1119,9 +1129,9 @@ abstract class LifecycleModule extends SimpleModule with Disposable { _willUnloadController.add(this); await Future.wait(_childModules.toList().map((child) { - child._parentContext = _activeSpan?.context; + _setParentContextOnChild(child, _activeSpan?.context); return child.unload().whenComplete(() { - child._parentContext = null; + _setParentContextOnChild(child, null); }); })); try { From 495788ffaaad647f4b33494b4a243911ec6ce272 Mon Sep 17 00:00:00 2001 From: Dusty Holmes Date: Fri, 29 Sep 2023 11:13:01 -0500 Subject: [PATCH 2/4] Run ci with 2.19.6 --- .github/workflows/dart_ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/dart_ci.yml b/.github/workflows/dart_ci.yml index dc9d130..1d0d037 100644 --- a/.github/workflows/dart_ci.yml +++ b/.github/workflows/dart_ci.yml @@ -15,7 +15,7 @@ jobs: strategy: fail-fast: false matrix: - sdk: [ 2.18.7 ] + sdk: [ 2.18.7 , 2.19.6] steps: - uses: actions/checkout@v2 - uses: dart-lang/setup-dart@v1.3 From 7b4b04d2512d936b72e6265a734f00f1a646ee0a Mon Sep 17 00:00:00 2001 From: Dusty Holmes Date: Fri, 29 Sep 2023 11:14:12 -0500 Subject: [PATCH 3/4] switch to a protected public member over the empty catch --- lib/src/lifecycle_module.dart | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/src/lifecycle_module.dart b/lib/src/lifecycle_module.dart index 3fc413c..814aab2 100644 --- a/lib/src/lifecycle_module.dart +++ b/lib/src/lifecycle_module.dart @@ -528,7 +528,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { try { manageDisposable(childModule); _childModules.add(childModule); - _setParentContextOnChild(childModule, _loadContext); + childModule.parentContext = _loadContext; await childModule.load(); try { @@ -554,7 +554,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { _didLoadChildModuleController.addError(error, stackTrace); completer.completeError(error, stackTrace); } finally { - _setParentContextOnChild(childModule, null); + childModule.parentContext = null; } }).catchError((Object error, StackTrace stackTrace) { _logger.severe( @@ -569,15 +569,11 @@ abstract class LifecycleModule extends SimpleModule with Disposable { return completer.future; } - /// This method is a workaround for the dangers of mock modules. A mock cannot implement a private member, so this will fail. - void _setParentContextOnChild( - LifecycleModule? childModule, - SpanContext? context, - ) { - try { - childModule?._parentContext = context; - } catch (e) {} - } + /// Provide a way for a module to update its children's parentContext that is compatible with mocking in 2.19. + /// + /// This is only intended for use within this file and is marked protected. + @protected + set parentContext(SpanContext? context) => _parentContext = context; /// Public method to suspend the module. /// @@ -1044,9 +1040,9 @@ abstract class LifecycleModule extends SimpleModule with Disposable { List> childResumeFutures = >[]; for (var child in _childModules.toList()) { childResumeFutures.add(Future.sync(() { - _setParentContextOnChild(child, _activeSpan?.context); + child.parentContext = _activeSpan?.context; return child.resume().whenComplete(() { - _setParentContextOnChild(child, null); + child.parentContext = null; }); })); } @@ -1081,9 +1077,9 @@ abstract class LifecycleModule extends SimpleModule with Disposable { List> childSuspendFutures = >[]; for (var child in _childModules.toList()) { childSuspendFutures.add(Future.sync(() async { - _setParentContextOnChild(child, _activeSpan?.context); + child.parentContext = _activeSpan?.context; return child.suspend().whenComplete(() { - _setParentContextOnChild(child, null); + child.parentContext = null; }); })); } @@ -1129,9 +1125,9 @@ abstract class LifecycleModule extends SimpleModule with Disposable { _willUnloadController.add(this); await Future.wait(_childModules.toList().map((child) { - _setParentContextOnChild(child, _activeSpan?.context); + child.parentContext = _activeSpan?.context; return child.unload().whenComplete(() { - _setParentContextOnChild(child, null); + child.parentContext = null; }); })); try { From 96665fb7c30813a96ad34a439786279bf55d1a31 Mon Sep 17 00:00:00 2001 From: Dusty Holmes Date: Fri, 29 Sep 2023 11:16:04 -0500 Subject: [PATCH 4/4] Remove extra spaces (formatting) --- lib/src/lifecycle_module.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/lifecycle_module.dart b/lib/src/lifecycle_module.dart index 814aab2..217e7c9 100644 --- a/lib/src/lifecycle_module.dart +++ b/lib/src/lifecycle_module.dart @@ -570,7 +570,7 @@ abstract class LifecycleModule extends SimpleModule with Disposable { } /// Provide a way for a module to update its children's parentContext that is compatible with mocking in 2.19. - /// + /// /// This is only intended for use within this file and is marked protected. @protected set parentContext(SpanContext? context) => _parentContext = context;