Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad state: Cannot add event after closing #111

Closed
Kleak opened this issue Aug 6, 2018 · 4 comments
Closed

Bad state: Cannot add event after closing #111

Kleak opened this issue Aug 6, 2018 · 4 comments
Assignees

Comments

@Kleak
Copy link

Kleak commented Aug 6, 2018

I got this error when i terminate a client and the client is listening to a Stream.

here is the proto:

syntax = "proto3";

package bug;

import "common.proto";

message Hello {
    string hello = 1;
}

service BugService {
    rpc infiniteStream (common.Empty) returns (stream Hello) {
    }
}

here is the service code :

class BugService extends BugServiceBase {
  final infiniteStreamController = StreamController.broadcast<Hello>();
  int count = 0;

  BugService() {
    Timer.periodic(Duration(seconds: 1), (_) {
      count++;
      infiniteStreamController.add(Hello()..hello = 'Hello $count');
    });
  }

  @override
  Stream<Hello> infiniteStream(ServiceCall call, Empty request) async* {
    await for (final hello in infiniteStreamController.stream) {
      yield hello;
    }
  }
}

and here is the client

Future<void> main() async {
  final channel = ClientChannel(
    'localhost',
    port: 8080,
    options: const ChannelOptions(
      credentials: const ChannelCredentials.insecure(),
    ),
  );
  final client = BugServiceClient(channel);
  final completer = Completer<void>();
  final subscription = client.infiniteStream(Empty()).listen(print);
  ProcessSignal.sigint.watch().listen((_) async {
    await subscription.cancel();
    print('quit gracefully');
    await channel.terminate();
    completer.complete();
  });
  await completer.future;
  exit(0);
}

and here is the stacktrace when the server crash

Unhandled exception:
Bad state: Cannot add event after closing
#0      _StreamController.add (dart:async/stream_controller.dart:584:24)
#1      _StreamSinkWrapper.add (dart:async/stream_controller.dart:858:13)
#2      TransportStream.sendHeaders (package:http2/transport.dart:241:10)
#3      ServerHandler.sendTrailers (package:grpc/src/server/handler.dart:301:13)
#4      ServerHandler._sendError (package:grpc/src/server/handler.dart:350:5)
#5      ServerHandler._onResponse (package:grpc/src/server/handler.dart:239:7)
#6      _RootZone.runUnaryGuarded (dart:async/zone.dart:1314:10)
#7      _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:336:11)
#8      _DelayedData.perform (dart:async/stream_impl.dart:584:14)
#9      _StreamImplEvents.handleNext (dart:async/stream_impl.dart:700:11)
#10     _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:660:7)
#11     _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
#12     _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
#13     _Timer._runTimers (dart:isolate/runtime/libtimer_impl.dart:391:30)
#14     _Timer._handleMessage (dart:isolate/runtime/libtimer_impl.dart:416:5)
#15     _RawReceivePortImpl._handleMessage (dart:isolate/runtime/libisolate_patch.dart:165:12)

Question:
How to stop sending value to the stream when the client quit ?

@Kleak
Copy link
Author

Kleak commented Aug 28, 2018

@sigurdm do you have an update about your fix ?

@a14n
Copy link
Contributor

a14n commented Nov 2, 2018

Should be fixed by dart-lang/http2#35 available in package http2-0.1.9.

@mit-mit
Copy link
Collaborator

mit-mit commented Nov 6, 2018

Thanks @a14n !, closing

@u007
Copy link

u007 commented May 20, 2020

im getting same error with version grpc '2.1.3'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants