Success WebSocket ping requests fail

[NSURLSessionWebSocketTask sendPingWithPongReceiveHandler:] on iOS does not seem to call the completion handler when called twice in a row with an intervening call to send or receive a message.

Here is a minimal reproduction from an Objective-C project.

- (void)viewDidLoad {
  [super viewDidLoad];

  NSURLSession* session = [NSURLSession sharedSession];
  NSURLSessionWebSocketTask* task = [session webSocketTaskWithRequest: [[NSURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:@"wss://ws.postman-echo.com/raw"]]];
  [task resume];
  [task sendPingWithPongReceiveHandler:^(NSError * _Nullable error) {
    printf("Got ping 1\n");  /* We will get to this line. */
    [task sendPingWithPongReceiveHandler:^(NSError * _Nullable error) {\
      printf("Got ping 2\n");  /* We will *not* get to this line. */
    }];
  }];
}

Using WireShark, I can verify that NSURLSession sends two pings and that the server responds with two pongs. But the callback handler does not get called after the second pong.

Does anyone have an suggestions on what I could be doing wrong?

That looks like a bug to me. Please file it as such, and then post your bug number, just for the record.

Network framework also supports WebSocket. You should repeat your test there to see if it has the same issue. If not, that’d be a nice workaround.

For a simple example of how to use Network framework’s WebSocket support, see this post.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

My use case requires that I reuse the configuration attached to NSURLSession so I'm not sure if using the Network framework would be an acceptable approach - I'll take a look.

I created filed 12179546

Thanks for the help!

Cheers, Brian

Success WebSocket ping requests fail
 
 
Q