Skip to content

Commit 08a4cd4

Browse files
committed
fix(web): Generate a session id before registering an input source
1 parent 1fd03c0 commit 08a4cd4

File tree

1 file changed

+9
-12
lines changed

1 file changed

+9
-12
lines changed

src/web/session.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ pub struct Session {
3333
impl Session {
3434
async fn json_api(&mut self, global: &Global) -> Result<&mut ClientConnection, SessionError> {
3535
if self.json_api.is_none() {
36+
// Generate a session ID
37+
if self.id.is_nil() {
38+
self.id = uuid::Uuid::new_v4();
39+
}
40+
3641
// Can't use SocketAddr, see https://github.com/seanmonstar/warp/issues/830
3742
self.json_api = Some(ClientConnection::new(
3843
global
@@ -157,18 +162,10 @@ impl<T: Reply> WithSession<T> {
157162
pub async fn new(reply: T, instance: SessionInstance) -> Self {
158163
let id = instance.session.read().await.id;
159164

160-
let set_cookie = if id.is_nil() {
161-
let mut session = instance.session.write().await;
162-
163-
if session.id == id {
164-
// Still the same ID after locking for write, so we can generate a new one
165-
let id = uuid::Uuid::new_v4();
166-
session.id = id;
167-
instance
168-
.sessions
169-
.write()
170-
.await
171-
.insert(id, instance.session.clone());
165+
let set_cookie = if !instance.sessions.read().await.contains_key(&id) {
166+
let mut sessions = instance.sessions.write().await;
167+
168+
if sessions.insert(id, instance.session.clone()).is_none() {
172169
Some(id.to_string())
173170
} else {
174171
// Not the same ID, another request set the cookie first

0 commit comments

Comments
 (0)