Skip to content

Commit 5ade3e3

Browse files
committed
fix(connection-selection): normalize connection string before caching in _persist_and_cache_connection
Strip whitespace and validate length before updating _connection_history_cache, ensuring the in-memory cache always holds the same normalized value that was persisted. Prevents blank/duplicate entries from surfacing in the combobox and being re-sent as preserved_connections on periodic port refresh.
1 parent 9a2f72a commit 5ade3e3

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

ardupilot_methodic_configurator/backend_filesystem_program_settings.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ def get_connection_history() -> list[str]:
648648
return ProgramSettings._connection_history.get_items(settings)
649649

650650
@staticmethod
651-
def store_connection(connection_string: str) -> None:
651+
def store_connection(connection_string: str) -> Optional[str]:
652652
"""
653653
Save a new connection string to history.
654654
@@ -660,14 +660,16 @@ def store_connection(connection_string: str) -> None:
660660
Args:
661661
connection_string: The connection string to store (max 200 characters).
662662
663-
Raises:
664-
ValueError: If connection string is invalid (logged as warning, not raised)
663+
Returns:
664+
The normalized connection string that was stored, or None if invalid.
665665
666666
"""
667667
try:
668668
settings = ProgramSettings._get_settings_as_dict()
669669
settings = ProgramSettings._connection_history.store_item(connection_string, settings)
670670
ProgramSettings._set_settings_from_dict(settings)
671+
return ProgramSettings._connection_history.normalizer(connection_string)
671672
except ValueError as e:
672673
# Log validation errors but don't raise (backward compatible behavior)
673674
logging_warning("Failed to store connection string to history: %s", e)
675+
return None

ardupilot_methodic_configurator/frontend_tkinter_connection_selection.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ def __init__( # pylint: disable=too-many-arguments, too-many-positional-argumen
124124

125125
def _persist_and_cache_connection(self, connection_string: str) -> None:
126126
"""Persist a connection string to settings and update the in-memory history cache."""
127-
ProgramSettings.store_connection(connection_string)
128-
# Update cache in-memory (most-recent-first, deduplicated) to avoid a disk re-read.
129-
self._connection_history_cache = [connection_string] + [
130-
c for c in self._connection_history_cache if c != connection_string
131-
]
127+
normalized = ProgramSettings.store_connection(connection_string)
128+
if normalized is None:
129+
return
130+
# Update cache in-memory (most-recent-first, deduplicated) using the normalized value
131+
# to avoid a disk re-read and to keep the cache consistent with what was persisted.
132+
self._connection_history_cache = [normalized] + [c for c in self._connection_history_cache if c != normalized]
132133

133134
def start_periodic_refresh(self) -> None:
134135
"""Start periodic refresh of available ports every 3 seconds."""

0 commit comments

Comments
 (0)