mirror of
https://github.com/servo/servo.git
synced 2025-07-19 13:23:46 +01:00
Devtools: send error replies instead of ignoring messages (#37686)
Client messages, which are always requests, are dispatched to Actor instances one at a time via Actor::handle_message. Each request must be paired with exactly one reply from the same actor the request was sent to, where a reply is a message with no type (if a message from the server has a type, it’s a notification, not a reply). Failing to reply to a request will almost always permanently break that actor, because either the client gets stuck waiting for a reply, or the client receives the reply for a subsequent request as if it was the reply for the current request. If an actor fails to reply to a request, we want the dispatcher (ActorRegistry::handle_message) to send an error of type `unrecognizedPacketType`, to keep the conversation for that actor in sync. Since replies come in all shapes and sizes, we want to allow Actor types to send replies without having to return them to the dispatcher. This patch adds a wrapper type around a client stream that guarantees request/reply invariants. It allows the dispatcher to check if a valid reply was sent, and guarantees that if the actor tries to send a reply, it’s actually a valid reply (see ClientRequest::is_valid_reply). It does not currently guarantee anything about messages sent via the TcpStream released via ClientRequest::try_clone_stream or the return value of ClientRequest::reply. We also send `unrecognizedPacketType`, `missingParameter`, `badParameterType`, and `noSuchActor` messages per the [protocol](https://firefox-source-docs.mozilla.org/devtools/backend/protocol.html#error-packets) [docs](https://firefox-source-docs.mozilla.org/devtools/backend/protocol.html#packets). Testing: automated tests all pass, and manual testing looks ok Fixes: #37683 and at least six bugs, plus one with a different root cause, plus three with zero impact --------- Signed-off-by: atbrakhi <atbrakhi@igalia.com> Signed-off-by: Delan Azabani <dazabani@igalia.com> Co-authored-by: delan azabani <dazabani@igalia.com> Co-authored-by: Simon Wülker <simon.wuelker@arcor.de> Co-authored-by: the6p4c <me@doggirl.gay>
This commit is contained in:
parent
fcb2a4cd95
commit
71d97bd935
36 changed files with 661 additions and 637 deletions
|
@ -2,16 +2,13 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::net::TcpStream;
|
||||
|
||||
use log::warn;
|
||||
use serde::Serialize;
|
||||
use serde_json::{Map, Value};
|
||||
use servo_config::pref;
|
||||
|
||||
use crate::StreamId;
|
||||
use crate::actor::{Actor, ActorMessageStatus, ActorRegistry};
|
||||
use crate::protocol::JsonPacketStream;
|
||||
use crate::actor::{Actor, ActorError, ActorRegistry};
|
||||
use crate::protocol::ClientRequest;
|
||||
|
||||
pub struct PreferenceActor {
|
||||
name: String,
|
||||
|
@ -30,43 +27,44 @@ impl Actor for PreferenceActor {
|
|||
|
||||
fn handle_message(
|
||||
&self,
|
||||
request: ClientRequest,
|
||||
_registry: &ActorRegistry,
|
||||
msg_type: &str,
|
||||
msg: &Map<String, Value>,
|
||||
stream: &mut TcpStream,
|
||||
_id: StreamId,
|
||||
) -> Result<ActorMessageStatus, ()> {
|
||||
let Some(key) = msg.get("value").and_then(|v| v.as_str()) else {
|
||||
warn!("PreferenceActor: handle_message: value is not a string");
|
||||
return Ok(ActorMessageStatus::Ignored);
|
||||
};
|
||||
) -> Result<(), ActorError> {
|
||||
let key = msg
|
||||
.get("value")
|
||||
.ok_or(ActorError::MissingParameter)?
|
||||
.as_str()
|
||||
.ok_or(ActorError::BadParameterType)?;
|
||||
|
||||
// TODO: Map more preferences onto their Servo values.
|
||||
Ok(match key {
|
||||
match key {
|
||||
"dom.serviceWorkers.enabled" => {
|
||||
self.write_bool(pref!(dom_serviceworker_enabled), stream)
|
||||
self.write_bool(request, pref!(dom_serviceworker_enabled))
|
||||
},
|
||||
_ => self.handle_missing_preference(msg_type, stream),
|
||||
})
|
||||
_ => self.handle_missing_preference(request, msg_type),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl PreferenceActor {
|
||||
fn handle_missing_preference(
|
||||
&self,
|
||||
request: ClientRequest,
|
||||
msg_type: &str,
|
||||
stream: &mut TcpStream,
|
||||
) -> ActorMessageStatus {
|
||||
) -> Result<(), ActorError> {
|
||||
match msg_type {
|
||||
"getBoolPref" => self.write_bool(false, stream),
|
||||
"getCharPref" => self.write_char("".into(), stream),
|
||||
"getIntPref" => self.write_int(0, stream),
|
||||
"getFloatPref" => self.write_float(0., stream),
|
||||
_ => ActorMessageStatus::Ignored,
|
||||
"getBoolPref" => self.write_bool(request, false),
|
||||
"getCharPref" => self.write_char(request, "".into()),
|
||||
"getIntPref" => self.write_int(request, 0),
|
||||
"getFloatPref" => self.write_float(request, 0.),
|
||||
_ => Err(ActorError::UnrecognizedPacketType),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_bool(&self, pref_value: bool, stream: &mut TcpStream) -> ActorMessageStatus {
|
||||
fn write_bool(&self, request: ClientRequest, pref_value: bool) -> Result<(), ActorError> {
|
||||
#[derive(Serialize)]
|
||||
struct BoolReply {
|
||||
from: String,
|
||||
|
@ -77,11 +75,10 @@ impl PreferenceActor {
|
|||
from: self.name.clone(),
|
||||
value: pref_value,
|
||||
};
|
||||
let _ = stream.write_json_packet(&reply);
|
||||
ActorMessageStatus::Processed
|
||||
request.reply_final(&reply)
|
||||
}
|
||||
|
||||
fn write_char(&self, pref_value: String, stream: &mut TcpStream) -> ActorMessageStatus {
|
||||
fn write_char(&self, request: ClientRequest, pref_value: String) -> Result<(), ActorError> {
|
||||
#[derive(Serialize)]
|
||||
struct CharReply {
|
||||
from: String,
|
||||
|
@ -92,11 +89,10 @@ impl PreferenceActor {
|
|||
from: self.name.clone(),
|
||||
value: pref_value,
|
||||
};
|
||||
let _ = stream.write_json_packet(&reply);
|
||||
ActorMessageStatus::Processed
|
||||
request.reply_final(&reply)
|
||||
}
|
||||
|
||||
fn write_int(&self, pref_value: i64, stream: &mut TcpStream) -> ActorMessageStatus {
|
||||
fn write_int(&self, request: ClientRequest, pref_value: i64) -> Result<(), ActorError> {
|
||||
#[derive(Serialize)]
|
||||
struct IntReply {
|
||||
from: String,
|
||||
|
@ -107,11 +103,10 @@ impl PreferenceActor {
|
|||
from: self.name.clone(),
|
||||
value: pref_value,
|
||||
};
|
||||
let _ = stream.write_json_packet(&reply);
|
||||
ActorMessageStatus::Processed
|
||||
request.reply_final(&reply)
|
||||
}
|
||||
|
||||
fn write_float(&self, pref_value: f64, stream: &mut TcpStream) -> ActorMessageStatus {
|
||||
fn write_float(&self, request: ClientRequest, pref_value: f64) -> Result<(), ActorError> {
|
||||
#[derive(Serialize)]
|
||||
struct FloatReply {
|
||||
from: String,
|
||||
|
@ -122,7 +117,6 @@ impl PreferenceActor {
|
|||
from: self.name.clone(),
|
||||
value: pref_value,
|
||||
};
|
||||
let _ = stream.write_json_packet(&reply);
|
||||
ActorMessageStatus::Processed
|
||||
request.reply_final(&reply)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue