Skip to content

Conversation

notoriaga
Copy link
Contributor

@notoriaga notoriaga commented Oct 18, 2021

And maybe CPP-328? Also seems to fix an issue (maybe this is happened when running via cargo run not sure) where closing the window would cause the processes running in the terminal to exit with an error. Now it cleanly shuts down

@notoriaga notoriaga force-pushed the steve/connection branch 4 times, most recently from f3abd32 to 5036250 Compare October 18, 2021 17:15
command = "${PYTHON}"
args = ["-m", "swiftnav_console.main", "${@}"]

[tasks.start-console-gdb]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How I've been debugging. Not sure if anyone has a better incantation but seems useful to have a command in the makefile for debugging

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could get python-pretty print for Rust using "rust-gdb".

command = "cargo"
cwd = "console_backend"
args = ["test", "--features", "tests", "--", "--nocapture"]
args = ["test", "--features", "tests", "${@}", "--", "--nocapture"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So you can run cargo make test <testname> to filter what tests to run

CLOSE,
#[strum(serialize = "CONNECTED")]
CONNECTED,
pub enum ApplicationState {
Copy link
Contributor Author

@notoriaga notoriaga Oct 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than two bools, connected/not connected + paused/not paused, this has just one ApplicationState (maybe ConnectionState is a better name?). So what was previously is_running is now matches!(state, CONNECTED | PAUSED).

CLOSING means the app is closing, either because close_when_done was set or a signal came from the frontend

DISCONNECTING is the time between a disconnect triggering and all the threads finishing joining

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of my todos in the other PR is to completely remove the paused logic hopefully that wont cause too much headache with this PR.

.handle_errors(move |e| {
debug!("{}", e);
match e {
sbp::DeserializeError::IoError(_) => ControlFlow::Break,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just breaking the loop is enough to disconnect the connection now

);
});
if conn.settings_enabled() {
let tab = SettingsTab::new(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the settings tab out of tabs. This makes it easier to run on its own thread. With this setup the settings tab is now completely separate from the main process messages loop. Previously the tick method was triggered on every new message, now it runs in response to the SettingsTabState changing. Doing it this way also makes it easier to skip building the settings tab for files, which don't need it. Can also skip on the hacky Mutex<Client> I had previously to avoid building the settings client when connected to a file

@notoriaga notoriaga force-pushed the steve/connection branch 2 times, most recently from 7e3e064 to 54941ec Compare October 18, 2021 17:23
@notoriaga
Copy link
Contributor Author

Have another branch to make this work with #167

@notoriaga notoriaga force-pushed the steve/connection branch 2 times, most recently from 2cf082a to 61eb01a Compare October 18, 2021 17:36
loop {
log::logger().flush();
let buf = server_recv.recv();
if let Ok(buf) = buf {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of this diff is just changing this if let into an early return to reduce the nesting


use parking_lot::{Condvar, Mutex};

pub struct Watched<T> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on - https://docs.rs/tokio/1.12.0/tokio/sync/watch/index.html

Like a spmc channel with capacity one. The difference is the channel only retains the last sent element, so sending never blocks. Receivers can get the latest value, or they can wait for a new value to appear. This makes it useful for broadcasting things where you only care about the latest value, like the app state, or the current connection.

@notoriaga notoriaga force-pushed the steve/connection branch 2 times, most recently from 4c9202c to 1d058c1 Compare October 18, 2021 19:59
Makefile.toml Outdated
"${PYTHON}",
"-m",
"swiftnav_console.main",
"--",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't this embedding of "--" found to be causing issues when using cli arguments?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right forgot to update that when I merged in main

command = "${PYTHON}"
args = ["-m", "swiftnav_console.main", "${@}"]

[tasks.start-console-gdb]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could get python-pretty print for Rust using "rust-gdb".

CLOSE,
#[strum(serialize = "CONNECTED")]
CONNECTED,
pub enum ApplicationState {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of my todos in the other PR is to completely remove the paused logic hopefully that wont cause too much headache with this PR.

@john-michaelburke
Copy link
Collaborator

For some reason whenever I start the app the connected button is already enabled. Are you not seeing this behavior?

@notoriaga
Copy link
Contributor Author

For some reason whenever I start the app the connected button is already enabled. Are you not seeing this behavior?

Oh yup fixed. Also I removed the pausing stuff, not sure if you got around to it in your pr but maybe you can avoid doing that?

@notoriaga notoriaga force-pushed the steve/connection branch 3 times, most recently from 4efe3fb to dbe5484 Compare October 19, 2021 19:27

/// Wrapper around the iterator of messages that enables other threads
/// to stop the iterator.
pub struct Messages {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously the only way to stop the main loop was to break inside of it. Now it can be stopped elsewhere which means we don't need to wait for a message to come in to disconnect.

So now if the internet drops and you stop getting messages you should still be able to disconnect

fn next(&mut self) -> Option<Self::Item> {
crossbeam::select! {
recv(self.canceled) -> _ => None,
recv(self.messages) -> msg => msg.ok(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could add a branch for a timeout here, then we could trigger a automatic reconnect or something

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I checked the auto reconnect does not work but does spit out: Releasing settings framework . Disconnecting manually then reconnecting does work which is great.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the current version supposed to reconnect? I didn't see any logic for that

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my message below.

@notoriaga notoriaga force-pushed the steve/connection branch 2 times, most recently from e52d8eb to 9dd98c0 Compare October 20, 2021 02:07
Comment on lines -238 to -263
while shared_state.clone().is_server_running() {
if let Ok(conn_option) = receiver.recv_timeout(Duration::from_secs_f64(
SERVER_STATE_CONNECTION_LOOP_TIMEOUT_SEC,
)) {
match conn_option {
Some(conn_) => {
conn = Some(conn_);
}
None => {
conn = None;
info!("Disconnected successfully.");
}
}
}
if let Some(conn_) = conn.clone() {
if let Err(e) =
process_messages(conn_, shared_state.clone(), client_send.clone())
{
error!("unable to process messages, {}", e);
}
if !shared_state.is_running() {
conn = None;
}
shared_state.set_running(false, client_send.clone());
}
log::logger().flush();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the bug we were having with the settings tab was preventing the reconnect logic from working but on tip of tree if you connect to a piksi (not the relay) and disconnect from the internet or lose connection to the piksi you would see the connect button flash on/off and spit out this "unable to process messages" until the connection successfully reconnected. The logic was a bit hacky but relied on whether or not shared_state.is_running was still set to true while looping here. If it was set to false it would assume the app was closing but if it was true it would keep calling the process_messages loop with the same connection where process_messages would perform "try_clone" on the connection. It seemed to do the trick for simple internet drops / piksi restarts but was likely not foolproof.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh that makes sense. Do you want me to re-implement the reconnect now or as a follow on? I don't think it'd take too long but merging this sooner rather than later is probably better for your stuff

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can push it to a future PR. This looks fine now :shipit:

@notoriaga notoriaga merged commit d689656 into main Oct 20, 2021
@notoriaga notoriaga deleted the steve/connection branch October 20, 2021 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants