diff --git a/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.cc b/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.cc index f811041ff0..41dcaef98a 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.cc +++ b/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.cc @@ -60,6 +60,7 @@ zx_handle_t Handle::ReleaseHandle() { zx_handle_t handle = handle_; handle_ = ZX_HANDLE_INVALID; + cached_koid_ = std::nullopt; while (waiters_.size()) { // HandleWaiter::Cancel calls Handle::ReleaseWaiter which removes the // HandleWaiter from waiters_. diff --git a/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.h b/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.h index 173cb7a5e5..9c9a7c7b7e 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.h +++ b/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/sdk_ext/handle.h @@ -7,6 +7,7 @@ #include +#include #include #include "flutter/fml/memory/ref_counted.h" @@ -52,10 +53,14 @@ class Handle : public fml::RefCountedThreadSafe, zx_handle_t handle() const { return handle_; } zx_koid_t koid() const { - zx_info_handle_basic_t info; - zx_status_t status = zx_object_get_info( - handle_, ZX_INFO_HANDLE_BASIC, &info, sizeof(info), nullptr, nullptr); - return status == ZX_OK ? info.koid : ZX_KOID_INVALID; + if (!cached_koid_.has_value()) { + zx_info_handle_basic_t info; + zx_status_t status = zx_object_get_info( + handle_, ZX_INFO_HANDLE_BASIC, &info, sizeof(info), nullptr, nullptr); + cached_koid_ = std::optional( + status == ZX_OK ? info.koid : ZX_KOID_INVALID); + } + return cached_koid_.value(); } zx_status_t Close(); @@ -90,6 +95,11 @@ class Handle : public fml::RefCountedThreadSafe, tonic::DartPersistentValue closure_string_; tonic::DartPersistentValue on_wait_completer_type_; tonic::DartPersistentValue schedule_microtask_string_; + + // Cache koid. To guarantee proper cache invalidation, only one `Handle` + // should own the `zx_handle_t` at a time and use of syscalls that invalidate + // the handle should use `ReleaseHandle()`. + mutable std::optional cached_koid_; }; } // namespace dart diff --git a/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/test/handle_test.dart b/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/test/handle_test.dart index ca9687a65a..79f9d25e81 100644 --- a/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/test/handle_test.dart +++ b/engine/src/flutter/shell/platform/fuchsia/dart-pkg/zircon/test/handle_test.dart @@ -153,4 +153,16 @@ void main() { expect(readResult.bytesAsUTF8String(), equals('\x00\x00')); }); }); + + test('cache koid and invalidate', () { + final HandleResult vmo = System.vmoCreate(0); + expect(vmo.status, equals(ZX.OK)); + int originalKoid = vmo.handle.koid; + expect(originalKoid, isNot(equals(ZX.KOID_INVALID))); + // Cached koid should be same value. + expect(originalKoid, equals(vmo.handle.koid)); + vmo.handle.close(); + // koid should be invalidated. + expect(vmo.handle.koid, equals(ZX.KOID_INVALID)); + }); }