-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dart: fix race condition between garbage collection of finalizable ob…
…jects and native function calls (#1633) ----- Motivation ----- If the generated class is not marked with 'Finalizable' interface, then, when methods are invoked on its object, the object can be garbage collected in the middle of method execution. For instance let's take a look at the following example from functional tests: 'AsyncClass().asyncVoid(false)' 1. AsyncClass object is created and its lifetime is tied with the native finalizer, which deletes shared_ptr (this.handle) that owns resources. 2. Then, inside 'asyncVoid(false)' we use only 'this.handle' member to pass the opaque handle to the native function. For garbage collector after the handle is passed 'this' object is no longer used by anybody. Therefore, it is not needed – can be finalized. 3. If the garbage collection kicks in before the native call, then the 'AsyncClass' object is garbage collected, its finalizer is run and therefore, the resources on C++ side are deleted. It causes the segmentation fault. We have a race condition between the garbage collection and execution of native functions. The consequences can be dramatic -- the finalizer registered for the object may be executed before the native function call inside the member methods. The finalizers delete C++ resources tied with the object. This can lead to segmentation fault. ----- Solution ----- The 'Finalizable' interface is a viable solution proposed by Dart:FFI documentation. It ensures, that 'this' object outlives the whole method call -- it guarantees, that the native function call finishes before the finalizer is run. Signed-off-by: Patryk Wrobel <183546751+pwrobeldev@users.noreply.github.com>
- Loading branch information
1 parent
97cd521
commit 2167a72
Showing
130 changed files
with
6,630 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.