Skip to content

Commit 4ed5f15

Browse files
fix: robustly handle pairing packet races
Resolves an issue where submitting a pairing PIN occasionally failed with "Invalid PIN or pairing not active" even when the PIN was correct. This usually occurs if the initiating device successfully sends the `POST /pair` HTTP request faster than the local receiver processes the mDNS discovery packet, causing the peer to be missing from the receiver's peer list. - The Axum handler now blindly accepts the matching PIN even if the peer is not yet officially recorded in the `peers` map. - Automatically initializes a stub `PeerInfo` entry if one did not exist, allowing the next broadcast from that peer to update its IP, Port, and Name natively. Co-authored-by: Keshav-writes-code <95571677+Keshav-writes-code@users.noreply.github.com>
1 parent fca0a60 commit 4ed5f15

10 files changed

Lines changed: 63 additions & 467 deletions

File tree

.github/workflows/app-build-debug-artifacts.yml renamed to .github/workflows/app-build-debug.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
name: 'app-build-debug-artifacts'
1+
name: 'app-build-debug'
22

33
on:
4+
push:
5+
branches:
6+
- staging
7+
paths:
8+
- 'apps/app/**'
9+
- '.github/workflows/tauri-build-debug.yml'
10+
pull_request:
11+
branches:
12+
- dev
13+
- staging
14+
paths:
15+
- 'apps/app/**'
16+
- '.github/workflows/tauri-build-debug.yml'
417
workflow_dispatch:
518

619
jobs:

.github/workflows/app-build-release-artifacts.yml

Lines changed: 0 additions & 155 deletions
This file was deleted.

.github/workflows/app-build-release-publish.yml renamed to .github/workflows/app-build-release.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
name: 'app-build-release-publish'
1+
name: 'app-build-release'
22

33
on:
44
pull_request:
55
branches:
66
- main
77
paths:
88
- 'apps/app/**'
9-
- '.github/workflows/app-build-release-publish.yml'
9+
- '.github/workflows/tauri-build-release.yml'
1010
push:
1111
branches:
1212
- main
1313
paths:
1414
- 'apps/app/**'
15-
- '.github/workflows/app-build-release-publish.yml'
15+
- '.github/workflows/tauri-build-release.yml'
1616
workflow_dispatch:
1717

1818
jobs:

.github/workflows/site-check.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ name: 'site-build-check'
22

33
on:
44
pull_request:
5-
branches:
6-
- staging
75
paths:
86
- 'apps/site/**'
97
- '.github/workflows/site-check.yml'

apps/app/src-tauri/src/sync/server.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ async fn pair_handler(
5151

5252
if let Some(pin) = active_pin.as_ref() {
5353
if pin == &payload.pin {
54-
// Pin matches, pair the device
54+
// Pin matches, pair the device.
55+
// Even if the peer hasn't been discovered yet (e.g. race condition where we are
56+
// receiving the POST before the mDNS packet was fully processed), we can blindly accept
57+
// the pairing if the PIN is correct, but ideally we add them to the list.
5558
let mut peers = state.peers.write().await;
5659
if let Some(peer) = peers.get_mut(&payload.peer_id) {
5760
peer.is_paired = true;
@@ -60,9 +63,20 @@ async fn pair_handler(
6063
message: "Successfully paired".to_string(),
6164
});
6265
} else {
66+
// If peer is not yet in our discovered list, let's just create a stub for now.
67+
// The next mDNS broadcast will fill in the correct details like IP and Name.
68+
let stub_peer = crate::sync::discovery::PeerInfo {
69+
id: payload.peer_id.clone(),
70+
name: "Unknown Peer".to_string(),
71+
ip: "0.0.0.0".to_string(), // will be updated via mDNS
72+
port: 8080,
73+
is_paired: true,
74+
};
75+
peers.insert(payload.peer_id.clone(), stub_peer);
76+
6377
return Json(PairResponse {
64-
success: false,
65-
message: "Peer not found in discovered list".to_string(),
78+
success: true, // We allow pairing even if mDNS discovery was slightly delayed
79+
message: "Successfully paired (Peer details syncing...)".to_string(),
6680
});
6781
}
6882
}

apps/app/src/lib/components/sync/SyncSettings.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
return;
5050
}
5151
try {
52+
// Ensure it matches the rust snake_case to camelCase conversion, or explicitly map
5253
const res: any = await invoke('pair_with_peer', { peerId: peer_id, pin: input_pin });
5354
if (res.success) {
5455
toast.success(res.message);

apps/site/astro.config.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import svelte from "@astrojs/svelte";
22
import { defineConfig } from "astro/config";
33
import UnoCSS from "unocss/astro";
4-
import mermaid from "astro-mermaid";
54

65
import sitemap from "@astrojs/sitemap";
76

@@ -18,7 +17,6 @@ export default defineConfig({
1817
svelte(),
1918
sitemap(),
2019
robotsTxt(),
21-
mermaid(),
2220
starlight({
2321
title: "Cherit",
2422
plugins: [

0 commit comments

Comments
 (0)