anchor.diff:    void sendNotification(Notification notification) {
       // Don't send any further notifications after the communication channel is
       // closed.
  -    if (_closed.isCompleted) {
  +    if (_closeRequested) {
         return;
       }
       ServerCommunicationChannel.ToJson.start();
       String jsonEncoding = JSON.encode(notification.toJson());
       ServerCommunicationChannel.ToJson.stop();
  -    output.write(jsonEncoding + '\n');
  +    _outputLine(jsonEncoding);
     }
   
     @override
     void sendResponse(Response response) {
       // Don't send any further responses after the communication channel is
       // closed.
  -    if (_closed.isCompleted) {
  +    if (_closeRequested) {
         return;
       }
       ServerCommunicationChannel.ToJson.start();
       String jsonEncoding = JSON.encode(response.toJson());
       ServerCommunicationChannel.ToJson.stop();
  -    output.write(jsonEncoding + '\n');
  +    _outputLine(jsonEncoding);
  +  }
  +
  +  /**
  +   * Callback invoked after a flush of [_output] completes.  Closes the stream
  +   * if necessary.  Otherwise schedules additional pending output.
  +   */
  +  void _flushCompleted(_) {
  +    assert(_outputState == _STATE_FLUSH_PENDING);
  +    if (_pendingOutput.isNotEmpty) {
  +      _output.write(_pendingOutput.join());
  +      _output.flush().then(_flushCompleted);
  +      _pendingOutput.clear();
  +      // Since we've done another flush, stay in _STATE_FLUSH_PENDING.
  +    } else {
  +      _outputState = _STATE_IDLE;
  +      if (_closeRequested) {
  +        assert(!_closed.isCompleted);
  +        _closed.complete();
  +      }
  +    }
  +  }
  +
  +  /**
  +   * Microtask that writes pending output to the output stream and flushes it.
  +   */
  +  void _microtask() {
  +    assert(_outputState == _STATE_MICROTASK_PENDING);
  +    _output.write(_pendingOutput.join());
  +    _output.flush().then(_flushCompleted);
  +    _pendingOutput.clear();
  +    _outputState = _STATE_FLUSH_PENDING;
  +  }
  +
  +  /**
  +   * Send the string [s] to [_output] followed by a newline.
  +   */
  +  void _outputLine(String s) {
  +    _pendingOutput.add(s);
  +    _pendingOutput.add('\n');
  +    if (_outputState == _STATE_IDLE) {
  +      // Don't send the output just yet; schedule a microtask to do it, so that
  +      // if caller decides to output additional lines, they will get sent in
  +      // the same call to _output.write().
  +      new Future.microtask(_microtask);
  +      _outputState = _STATE_MICROTASK_PENDING;
  +    }
     }
   
     /**
  
positive.code:  * standard input and standard output) to communicate with clients.
   */
  class ByteStreamServerChannel implements ServerCommunicationChannel {
    final Stream input;
    final IOSink output;
  
    /**
     * Completer that will be signalled when the input stream is closed.
     */
    final Completer _closed = new Completer();
  
    ByteStreamServerChannel(this.input, this.output);
  
    /**
     * Future that will be completed when the input stream is closed.
  
negative.code: 
    @override
    void close() {
      if (!_closeRequested) {
        _closeRequested = true;
        if (_outputState == _STATE_IDLE) {
          assert(!_closed.isCompleted);
          _closed.complete();
        } else {
          // Nothing to do.  [_flushCompleted] will call _closed.complete() after
          // the flush completes.
        }
      }
    }
  
    @override
  