Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/system/containers.toit
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ class Container extends ServiceResourceProxy:
constructor.internal_ --handle/int --.id --.gid --on-event/Lambda? --on-stopped/Lambda?:
on-event_ = on-event
on-stopped_ = on-stopped
super _client_ handle
super _client_ handle --install-finalizer=(not (on-stopped or on-event))

close -> none:
catch --trace: throw "close"
// Make sure anyone waiting for the result now or in the future
// knows that we got closed before getting an exit code.
if not result_.has-value: result_.set null
Expand Down
4 changes: 2 additions & 2 deletions lib/system/services.toit
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,8 @@ abstract class ServiceResourceProxy:
client_/ServiceClient ::= ?
_handle_/int? := ?

constructor .client_ ._handle_:
add-finalizer this:: close
constructor .client_ ._handle_ --install-finalizer/bool=true:
if install-finalizer: add-finalizer this:: close
if _handle_ & 1 == 1:
ServiceResourceProxyManager_.instance.register client_.id _handle_ this

Expand Down
11 changes: 11 additions & 0 deletions tests/containers-test.toit
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import monitor
import system.containers
import system
import expect show *

main arguments:
Expand All @@ -17,6 +18,7 @@ main arguments:
test-images
test-start
test-background-state-changed
test-finalizer

test-images:
images/List := containers.images
Expand Down Expand Up @@ -61,6 +63,15 @@ test-start:
expect-equals 0 lambda4-value
expect-equals 0 sub4.wait

test-finalizer:
// A container should receive the stop notification even if we don't have
// any pointer to the actual container anymore.
latch := monitor.Latch
containers.start containers.current {:} --on-stopped=(:: latch.set true)
// Force a GC.
system.process-stats --gc
with-timeout --ms=5_000: latch.get

test-background-state-changed:
channel := monitor.Channel 1
sub := containers.start containers.current { "background-state-test": true }
Expand Down
Loading