spawn_worker_print_fd.mx raw

   1  //:wasmtest skip
   2  // Native-only: a worker's print has no path to the host console on wasm; the
   3  // wasm model needs a channel from every worker to the main page script, which
   4  // this fixture does not build. The native assertion is the printFD seed.
   5  // A spawned domain must be able to print. Runtime package variables are not
   6  // re-initialised through the package initialisers in a new domain, so their
   7  // static initialisers never fired: printFD stayed 0 and every println or
   8  // runtime print in a worker wrote to fd 0 (read-only) and vanished, which made
   9  // every domain-level diagnostic invisible. initDomain seeds it now; this pins
  10  // the seed by asking the worker for the descriptor it prints through.
  11  package main
  12  
  13  import "runtime"
  14  
  15  func worker(out chan int32) (closed chan struct{}) {
  16  	select {
  17  	case <-closed:
  18  		return closed
  19  	default:
  20  	}
  21  	println("worker canary")
  22  	out <- runtime.PrintFDValue()
  23  	return closed
  24  }
  25  
  26  func main() {
  27  	select {
  28  	case <-shutdown:
  29  		return
  30  	default:
  31  	}
  32  	out := chan int32{:2}
  33  	done := spawn(worker, out)
  34  	fd := <-out
  35  	if fd != 1 {
  36  		panic("worker print descriptor is not 1")
  37  	}
  38  	<-done
  39  	println("spawn print ok")
  40  }
  41