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>
125 lines
3.9 KiB
C++
125 lines
3.9 KiB
C++
// Copyright (c) 2012, 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 <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "include/bin/dart_io_api.h"
|
|
#include "include/dart_api.h"
|
|
#include "include/dart_tools_api.h"
|
|
|
|
#include "platform/assert.h"
|
|
|
|
#include "bin/builtin.h"
|
|
#include "bin/dartutils.h"
|
|
#include "bin/file.h"
|
|
#include "bin/io_natives.h"
|
|
#include "bin/platform.h"
|
|
|
|
namespace dart {
|
|
namespace bin {
|
|
|
|
// Lists the native functions implementing basic functionality in
|
|
// standalone dart, such as printing, file I/O, and platform information.
|
|
// Advanced I/O classes like sockets and process management are implemented
|
|
// using functions listed in io_natives.cc.
|
|
#define BUILTIN_NATIVE_LIST(V) V(Builtin_PrintString, 1)
|
|
|
|
BUILTIN_NATIVE_LIST(DECLARE_FUNCTION);
|
|
|
|
static struct NativeEntries {
|
|
const char* name_;
|
|
Dart_NativeFunction function_;
|
|
int argument_count_;
|
|
} BuiltinEntries[] = {BUILTIN_NATIVE_LIST(REGISTER_FUNCTION)};
|
|
|
|
void Builtin_DummyNative(Dart_NativeArguments args) {
|
|
UNREACHABLE();
|
|
}
|
|
|
|
/**
|
|
* Looks up native functions in both libdart_builtin and libdart_io.
|
|
*/
|
|
Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name,
|
|
int argument_count,
|
|
bool* auto_setup_scope) {
|
|
const char* function_name = nullptr;
|
|
Dart_Handle err = Dart_StringToCString(name, &function_name);
|
|
if (Dart_IsError(err)) {
|
|
Dart_PropagateError(err);
|
|
}
|
|
ASSERT(function_name != nullptr);
|
|
ASSERT(auto_setup_scope != nullptr);
|
|
*auto_setup_scope = true;
|
|
int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries);
|
|
for (int i = 0; i < num_entries; i++) {
|
|
struct NativeEntries* entry = &(BuiltinEntries[i]);
|
|
if ((strcmp(function_name, entry->name_) == 0) &&
|
|
(entry->argument_count_ == argument_count)) {
|
|
return reinterpret_cast<Dart_NativeFunction>(entry->function_);
|
|
}
|
|
}
|
|
Dart_NativeFunction result =
|
|
IONativeLookup(name, argument_count, auto_setup_scope);
|
|
if (result == nullptr) {
|
|
result = Builtin_DummyNative;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void* Builtin::FfiNativeLookup(const char* name, uintptr_t argument_count) {
|
|
return IOFfiNativeLookup(name, argument_count);
|
|
}
|
|
|
|
const uint8_t* Builtin::NativeSymbol(Dart_NativeFunction nf) {
|
|
int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries);
|
|
for (int i = 0; i < num_entries; i++) {
|
|
struct NativeEntries* entry = &(BuiltinEntries[i]);
|
|
if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) {
|
|
return reinterpret_cast<const uint8_t*>(entry->name_);
|
|
}
|
|
}
|
|
return IONativeSymbol(nf);
|
|
}
|
|
|
|
// Implementation of native functions which are used for some
|
|
// test/debug functionality in standalone dart mode.
|
|
void FUNCTION_NAME(Builtin_PrintString)(Dart_NativeArguments args) {
|
|
intptr_t length = 0;
|
|
Dart_Handle str = Dart_GetNativeArgument(args, 0);
|
|
Dart_Handle result = Dart_StringUTF8Length(str, &length);
|
|
if (Dart_IsError(result)) {
|
|
Dart_PropagateError(result);
|
|
}
|
|
intptr_t new_length = length + 1;
|
|
uint8_t* chars = Dart_ScopeAllocate(new_length);
|
|
ASSERT(chars != nullptr);
|
|
result = Dart_CopyUTF8EncodingOfString(str, chars, length);
|
|
if (Dart_IsError(result)) {
|
|
Dart_PropagateError(result);
|
|
}
|
|
chars[length] = '\n';
|
|
|
|
// Uses fwrite to support printing NUL bytes.
|
|
const uint8_t* cursor = chars;
|
|
intptr_t remaining = new_length;
|
|
while (remaining > 0) {
|
|
intptr_t written = fwrite(cursor, 1, remaining, stdout);
|
|
if (written == 0) break; // EOF or error: ignore
|
|
cursor += written;
|
|
remaining -= written;
|
|
}
|
|
fflush(stdout);
|
|
if (ShouldCaptureStdout()) {
|
|
// For now we report print output on the Stdout stream.
|
|
const char* res =
|
|
Dart_ServiceSendDataEvent("Stdout", "WriteEvent", chars, new_length);
|
|
ASSERT(res == nullptr);
|
|
}
|
|
}
|
|
|
|
} // namespace bin
|
|
} // namespace dart
|