main.mx raw

   1  // A panic in a spawned wasm domain ends the WHOLE tree, the way an abort ends
   2  // every domain in a native process. The page learns the message as stderr and
   3  // stops every Worker it spawned for this module.
   4  //
   5  // The fixture makes both halves observable: one spawned domain panics, and a
   6  // sibling would print "sibling survived" 1.5 s later. The harness runs the
   7  // module, waits three seconds, and requires the panic line on stderr and the
   8  // absence of the sibling line - a page that merely stopped the failing domain
   9  // would forward the sibling's print through the still-live root worker.
  10  package main
  11  
  12  import "time"
  13  
  14  func panicker(trigger chan int32) (closed chan struct{}) {
  15  	for {
  16  		select {
  17  		case <-closed:
  18  			return closed
  19  		case <-trigger:
  20  			panic("sibling died")
  21  		}
  22  	}
  23  }
  24  
  25  // hold busy-waits, because time.Sleep is a no-op on wasm (sleepTicks is
  26  // empty: a Worker cannot yield to a scheduler it does not have). It runs in
  27  // this domain's own thread, and it must not allocate per iteration.
  28  func hold() {
  29  	deadline := time.Now().Add(1500 * time.Millisecond)
  30  	for time.Now().Before(deadline) {
  31  	}
  32  }
  33  
  34  func survivor(wake chan int32) (closed chan struct{}) {
  35  	for {
  36  		select {
  37  		case <-closed:
  38  			return closed
  39  		case <-wake:
  40  			hold()
  41  			println("sibling survived")
  42  			return closed
  43  		}
  44  	}
  45  }
  46  
  47  func main() {
  48  	select {
  49  	case <-shutdown:
  50  		return
  51  	default:
  52  	}
  53  	trigger := chan int32{}
  54  	wake := chan int32{}
  55  	spawn(panicker, trigger)
  56  	spawn(survivor, wake)
  57  	trigger <- 1
  58  	wake <- 1
  59  	// Return. A root domain that parks in Atomics.wait stops forwarding its
  60  	// children's messages (their postMessage sits in this Worker's queue), so
  61  	// the page would never see the panic. Returning lets this Worker's event
  62  	// loop run while the page keeps the root alive.
  63  }
  64