ed6bab847b
Existing implementation is an entangled mess which consists of shared code residing in the base class which in random places invokes a number of undocumented poorly named methods overloaded in OS specific subclasses. Some of these methods mutate static state. There are no clear lifetime guarantees for different parts of the system (including comments saying that some values might or might not be valid at certain points). The rewrite aims to clean most of this up - sharing everything that can be shared and moving OS specific logic to clearly documented methods. Furthermore, we change the code to ensure proper lifetime guarantees - so we no longer find ourself in situations where we don't know whether pathId is valid or not. This refactoring by itself fixes a number of issues, most specifically a bug where watcher would stop receiving events on Windows because DirectoryWatchHandle ends up allocated at precisely the same address as a previous destroyed one - which confuses Dart side to think that newly created handle is the same as the old one (due to a race between event handler thread and Dart thread). We fix Windows lifetime issue by a) not keeping pathId based mapping in the watcher anymore and b) keeping DirectoryWatchHandler alive until it is stoped by the Dart side - this is achieved by retaining it after it is created and releasing it once path is unwatched. This way Dart side is always sure that pathId values are valid until they are explicitly released via _unwatchPath - which makes code very uniform. To make sure that native objects created by _watchPath are released when surrounding isolate exists abruptly (e.g. via Isolate.exit - without letting Dart code to shutdown and call _unwatchPath naturally) we attach NativeFinalizer to them. This fixes the existing leak of file watchers on Mac OS X - as Node objects it created were not freed if surrounding isolate exited. Note that inotify descriptors did not leak in the same way because they were wrapped into sockets. Finally, this refactoring also make sure that the last subscriber cancelling subscription on filesystem event stream will get a proper cancellation future back and can wait for the watcher to shutdown. Previously implementation used broadcast streams which simply return an already completed future when subscriber cancels. New implementation uses Stream.multi instead which gives a better result. Now doing watch().listen().cancel() returns a future which will only complete once watcher is fully disposed (e.g. inotify descriptor is closed). Bad behavior was revealed by analysing standalone/regress_52715 - which revealed that repeatedly watching and cancelling might flakely cause us to hit fd limit depending on whether eventhandler thread can keep up closing file descriptors created by the main thread or not. Fixes https://github.com/dart-lang/sdk/issues/61378 TEST=standalone/{regress_61378,file_system_watcher_isolate_exit_leak} CoreLibraryReviewExempt: VM only changes. Change-Id: I6a6a69642b1f2673f2be78434bc64270846ad8c5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/450921 Reviewed-by: Lasse Nielsen <lrn@google.com>
69 lines
2.6 KiB
C++
69 lines
2.6 KiB
C++
// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include "bin/file_system_watcher.h"
|
|
|
|
#include "bin/builtin.h"
|
|
#include "bin/dartutils.h"
|
|
#include "bin/file.h"
|
|
#include "bin/utils.h"
|
|
|
|
#include "include/dart_api.h"
|
|
|
|
namespace dart {
|
|
namespace bin {
|
|
|
|
bool FileSystemWatcher::delayed_filewatch_callback_ = false;
|
|
|
|
void FUNCTION_NAME(FileSystemWatcher_IsSupported)(Dart_NativeArguments args) {
|
|
Dart_SetBooleanReturnValue(args, FileSystemWatcher::IsSupported());
|
|
}
|
|
|
|
void FUNCTION_NAME(FileSystemWatcher_InitWatcher)(Dart_NativeArguments args) {
|
|
intptr_t id = FileSystemWatcher::Init();
|
|
if (id >= 0) {
|
|
Dart_SetReturnValue(args, Dart_NewInteger(id));
|
|
} else {
|
|
Dart_ThrowException(DartUtils::NewDartOSError());
|
|
}
|
|
}
|
|
|
|
void FUNCTION_NAME(FileSystemWatcher_WatchPath)(Dart_NativeArguments args) {
|
|
intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 0));
|
|
Namespace* namespc = Namespace::GetNamespace(args, 1);
|
|
const char* path = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 2));
|
|
int events = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3));
|
|
bool recursive = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 4));
|
|
intptr_t path_id =
|
|
FileSystemWatcher::WatchPath(id, namespc, path, events, recursive);
|
|
if (path_id == -1) {
|
|
Dart_ThrowException(DartUtils::NewDartOSError());
|
|
}
|
|
Dart_SetIntegerReturnValue(args, path_id);
|
|
}
|
|
|
|
void FUNCTION_NAME(FileSystemWatcher_UnwatchPath)(Dart_NativeArguments args) {
|
|
intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 0));
|
|
intptr_t path_id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1));
|
|
FileSystemWatcher::UnwatchPath(id, path_id);
|
|
}
|
|
|
|
void FUNCTION_NAME(FileSystemWatcher_ReadEvents)(Dart_NativeArguments args) {
|
|
intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 0));
|
|
intptr_t path_id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1));
|
|
Dart_Handle handle = FileSystemWatcher::ReadEvents(id, path_id);
|
|
ThrowIfError(handle);
|
|
Dart_SetReturnValue(args, handle);
|
|
}
|
|
|
|
void FUNCTION_NAME(FileSystemWatcher_GetSocketId)(Dart_NativeArguments args) {
|
|
intptr_t id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 0));
|
|
intptr_t path_id = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 1));
|
|
intptr_t socket_id = FileSystemWatcher::GetSocketId(id, path_id);
|
|
Dart_SetIntegerReturnValue(args, socket_id);
|
|
}
|
|
|
|
} // namespace bin
|
|
} // namespace dart
|