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

Proactive disconnect reporting #85

Merged
merged 6 commits into from
Oct 4, 2018
Merged

Proactive disconnect reporting #85

merged 6 commits into from
Oct 4, 2018

Conversation

cwillisf
Copy link
Contributor

@cwillisf cwillisf commented Oct 2, 2018

Review round 2:

This new version is still sending the error notification to the client, but also closes the web socket. This means that the overall behavior is now guaranteed correct from a "1 session = 1 socket = 1 peripheral" point of view without the client doing anything special for this situation.

Review round 1:

This change ensures that the client (extension / extension framework) is notified about a connection problem as soon as possible. Previously, in many cases, the extension didn't hear about a connection problem until it tried to perform a read or write on the peripheral, which depending on the extension may happen only rarely.

In general this is handled by sending an error notification to the client. To make this easier, I pulled some of the response construction code out into a function that could be called both by the regular response path and the error notification path.

Note that BLE on Windows was already working well, so changes were only necessary for BT on Windows and both BLE and BT on Mac.

@@ -187,7 +192,7 @@ class BTSession: Session, IOBluetoothRFCOMMChannelDelegate, IOBluetoothDeviceInq

func deviceInquiryDeviceNameUpdated(
_ sender: IOBluetoothDeviceInquiry!, device: IOBluetoothDevice!, devicesRemaining: UInt32) {
print("name updated: \(device.name)")
print("name updated: \(String(describing: device.name))")

This comment was marked as abuse.

thisandagain
thisandagain previously approved these changes Oct 2, 2018
Copy link
Contributor

@thisandagain thisandagain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. It would be nice to do another round of testing with these changes this week.

new JProperty("id", responseId),
error == null ? new JProperty("result", result) : new JProperty("error", JObject.FromObject(error))
);
var response = MakeResponse(responseId, result, error);

This comment was marked as abuse.

@evhan55
Copy link
Contributor

evhan55 commented Oct 3, 2018

I just made a new build with this branch and tried the following:

  • Load WeDo2 extension (BLE on Mac)
  • Connect to WeDo2
  • Test that it's working by clicking turn motor for 1 second
  • Take batteries out of WeDo2
  • Wait 10 seconds

Expected

  • Error comes back from Scratch Link and disconnecting alerts show / status icon turns orange

Actual

  • Nothing happened. Everything stayed the same and it looked like it was still connected

Was this case supposed to be addressed with this branch's change?

Scratch Link Build No. : 1.18.1002.945

@cwillisf
Copy link
Contributor Author

cwillisf commented Oct 3, 2018

@evhan55 Interesting. I mostly tested with the micro:bit and EV3 so maybe the WeDo 2's behavior is different somehow. I wouldn't expect so, but... maybe? Anyway, I'll see if I can repro. Thanks!

Just to clarify expectations: I would expect Scratch Link to send an error but not actually disconnect the socket. I would expect the extension to disconnect the socket in response to the error.

@evhan55
Copy link
Contributor

evhan55 commented Oct 3, 2018

@cwillisf

The error that it sends back, if it's not in response to a write/read, then is it something that would be documented and can be handled in didReceiveCall?

@cwillisf
Copy link
Contributor Author

cwillisf commented Oct 3, 2018

Based on discussion with @evhan55 I'm going to try a slightly different approach... to be continued!

@cwillisf cwillisf dismissed thisandagain’s stale review October 3, 2018 23:56

Significant changes coming...

@evhan55
Copy link
Contributor

evhan55 commented Oct 4, 2018

Trying build version:

Scratch Link 1.18.1004.15 c1cb595
macOS Version 10.14 (Build 18A391)

Still doesn't seem to auto-close the socket on the Chrome side of things.
Happy to help debug, I will be more available tomorrow!

Question: how long should I be waiting for the auto-close?
(I think I waited about 6 minutes at one point)

@cwillisf
Copy link
Contributor Author

cwillisf commented Oct 4, 2018

Hmm... it should auto-close more or less immediately. In my experience it happens in less than a second.
Were you testing on Mac with a WeDo 2?

@evhan55
Copy link
Contributor

evhan55 commented Oct 4, 2018

Yes! Mac OS Mojave with WeDo 2.

@evhan55
Copy link
Contributor

evhan55 commented Oct 4, 2018

I re-tested with a fresh build from this branch:

  • Load WeDo2 extension (BLE on Mac)
  • Connect to WeDo2
  • Test that it's working by clicking turn motor for 1 second
  • Take batteries out of WeDo2
  • Wait 2 seconds
  • WeDo2 disconnects from OS (bluetooth icon in taskbar switches to 'disconnected')

Expected: Scratch VM gets the web socket closed error and dispatches error alerts

Actual: Nothing happens until I click a turn motor on for 1 second block, then the VM disconnects and dispatches error alerts

Scratch Link Version:

Scratch Link 1.18.1004.15 c1cb595
macOS Version 10.14 (Build 18A391)

@cwillisf
Copy link
Contributor Author

cwillisf commented Oct 4, 2018

@evhan55: OK, now that I understand what you're seeing I see the same thing. Unfortunately I don't currently see a way to catch this. The OS seems to notice that the peripheral has gone away, but none of the APIs that I'm using seem to report it at the Swift / CoreBluetooth level.

It does seem to report an error and disconnect reliably if your computer falls asleep or if the user disables Bluetooth, though.

@cwillisf
Copy link
Contributor Author

cwillisf commented Oct 4, 2018

In the interest of getting a new build into MAS review I'm going to ahead and merge this for now. I filed #87 to track the issue from the conversation above.

@cwillisf cwillisf merged commit db6e35d into scratchfoundation:develop Oct 4, 2018
@cwillisf cwillisf deleted the proactive-disconnect-reporting branch October 4, 2018 22:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants