Auto merge of #12518 - creativcoder:swm-fix, r=nox

Remove expect calls in service worker manager thread

<!-- Please describe your changes on the following line: -->

An intermittent timeout was seen in #12516 , caused by panic of ServiceWorkerManager thread on reception of messages from resource thread. This PR amends things to not have the ServiceWorkerManager thread panic in such situation.
cc @jdm

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [X] These changes fix ##12516 (github issue number if applicable).

<!-- Either: -->
- [X] These changes do not require tests because its an "intermittent fix"

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/12518)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-07-21 10:17:47 -05:00 committed by GitHub
commit 07a0c2f1dc

View file

@ -15,10 +15,15 @@ use ipc_channel::router::ROUTER;
use net_traits::{CustomResponseMediator, CoreResourceMsg};
use script_traits::{ServiceWorkerMsg, ScopeThings, SWManagerMsg, SWManagerSenders};
use std::collections::HashMap;
use std::sync::mpsc::{channel, Receiver};
use std::sync::mpsc::{channel, Receiver, RecvError};
use url::Url;
use util::thread::spawn_named;
enum Message {
FromResource(CustomResponseMediator),
FromConstellation(ServiceWorkerMsg)
}
pub struct ServiceWorkerManager {
// map of registered service worker descriptors
registered_workers: HashMap<Url, ScopeThings>,
@ -102,8 +107,18 @@ impl ServiceWorkerManager {
}
fn handle_message(&mut self) {
while self.receive_message() {
// process message
while let Ok(message) = self.receive_message() {
let should_continue = match message {
Message::FromConstellation(msg) => {
self.handle_message_from_constellation(msg)
},
Message::FromResource(msg) => {
self.handle_message_from_resource(msg)
}
};
if !should_continue {
break;
}
}
}
@ -140,24 +155,12 @@ impl ServiceWorkerManager {
}
#[allow(unsafe_code)]
fn receive_message(&mut self) -> bool {
enum Message {
FromResource(CustomResponseMediator),
FromConstellation(ServiceWorkerMsg)
}
let message = {
fn receive_message(&mut self) -> Result<Message, RecvError> {
let msg_from_constellation = &self.own_port;
let msg_from_resource = &self.resource_receiver;
select! {
msg = msg_from_constellation.recv() =>
Message::FromConstellation(msg.expect("Unexpected constellation channel panic in sw-manager")),
msg = msg_from_resource.recv() =>
Message::FromResource(msg.expect("Unexpected resource channel panic in sw-manager"))
}
};
match message {
Message::FromConstellation(msg) => self.handle_message_from_constellation(msg),
Message::FromResource(mediator) => self.handle_message_from_resource(mediator)
msg = msg_from_constellation.recv() => msg.map(Message::FromConstellation),
msg = msg_from_resource.recv() => msg.map(Message::FromResource)
}
}
}