Skip to content

check signals in nu-glob and ls - #15140

Merged
sholderbach merged 1 commit into
nushell:mainfrom
sgvictorino:glob-interruption
Feb 28, 2025
Merged

sholderbach merged 1 commit into
nushell:mainfrom
sgvictorino:glob-interruption

Conversation

@sgvictorino

Copy link
Copy Markdown
Contributor

Fixes #10144

User-Facing Changes

Long running glob expansions and ls runs (e.g. ls /**/*) can now be
interrupted with ctrl-c.

@sgvictorino
sgvictorino marked this pull request as ready for review February 19, 2025 00:50

@sholderbach sholderbach left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for tackling this!

Comment thread crates/nu-glob/Cargo.toml
bench = false

[dependencies]
nu-protocol = { path = "../nu-protocol", version = "0.102.1", default-features = false }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We kind of need to be careful here, when it comes to circular imports. nu-engine as the next crate down the line of nu-protocol already depends on nu-glob. So far it doesn't seem to cause trouble, but in the future (or whenever we can carry @devyn 's platform revised platform-tolerant and fancy glob library across the finish line) we may have to watch out for that.

Comment thread crates/nu-glob/src/lib.rs Outdated
children.sort_by(|p1, p2| p2.file_name().cmp(&p1.file_name()));
children.sort_by(|p1, p2| {
if signals.interrupted() {
std::cmp::Ordering::Equal

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this Ordering::Equal cause any weirdness?

I think we should check below again for signals.interrupted and explicitly return the error.

@sgvictorino sgvictorino Feb 23, 2025 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think it'll cause problems because we don't use the result. Here's a contrived benchmark that demonstrates the improvement when we're interrupted in this code path:

mkdir ~/bigdir
cd ~/bigdir
seq 1 1000000 | par-each { || touch (random chars) }

cd <nushell working dir>
gh pr checkout 15140

def bench [] { cargo build; timeit { do -i { ./target/debug/nu -c "ls ~/bigdir/*" } } }

"--- a/crates/nu-glob/src/lib.rs
+++ b/crates/nu-glob/src/lib.rs
@@ -1011,13 +1011,8 @@ fn fill_todo(
                     //             .starts_with('.')
                     //     });
                     // }
-                    children.sort_by(|p1, p2| {
-                        if signals.interrupted() {
-                            Ordering::Equal
-                        } else {
-                            p2.file_name().cmp(&p1.file_name())
-                        }
-                    });
+                    nix::sys::signal::raise(nix::sys::signal::SIGINT).unwrap();
+                    children.sort_by(|p1, p2| p2.file_name().cmp(&p1.file_name()));
                     if signals.interrupted() {
                         todo.push(Err(GlobError {
                             path: path.to_path_buf(),
" | save /tmp/bench-slow.diff
git apply /tmp/bench-slow.diff
bench
# 2sec 819ms 882µs 883ns

"--- a/crates/nu-glob/src/lib.rs
+++ b/crates/nu-glob/src/lib.rs
@@ -1012,7 +1012,13 @@ fn fill_todo(
                     //     });
                     // }
                     nix::sys::signal::raise(nix::sys::signal::SIGINT).unwrap();
-                    children.sort_by(|p1, p2| p2.file_name().cmp(&p1.file_name()));
+                    children.sort_by(|p1, p2| {
+                        if signals.interrupted() {
+                            Ordering::Equal
+                        } else {
+                            p2.file_name().cmp(&p1.file_name())
+                        }
+                    });
                     if signals.interrupted() {
                         todo.push(Err(GlobError {
                             path: path.to_path_buf(),
" | save /tmp/speedup.diff
git apply /tmp/speedup.diff
bench
# 681ms 702µs 760ns

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

While I understand the potential for speedup, I think sort_by may choose to panic if the interrupt changes the sort order mid-operation.

Changing a relation of three elements a b c from some ordering to Equals can violate both the transitivity and duality properties for PartialOrd and the rules for Ord

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh, good catch! That's unfortunate.

Comment thread crates/nu-command/src/filesystem/ls.rs Resolved
Fixes #10144

# User-Facing Changes

Long running glob expansions and `ls` runs (e.g. `ls /**/*`) can now be
interrupted with ctrl-c.
@sholderbach

Copy link
Copy Markdown
Member

Are we through this?

Just as a heads up from a reviewing point of view, force-pushed amended commits are really hard to follow when a conversation is happening, as we squash at the end its fine to leave an iterative history. (I am fine with a force-push when things are rebased onto larger upstream change and its more readable than a big merge commit blob which GH doesn't render well, or if you tweaked one particular commit due to formatting or whatever in a larger history)

@sgvictorino

sgvictorino commented Feb 27, 2025 •

Copy link
Copy Markdown
Contributor Author

I think I addressed your comments.

Sorry about the big rebase! Here's a post-review diff with the original base commit: https://github.com/nushell/nushell/compare/43bdf607cd7b26cd2d7083e230c3b2753abb58d3..e9bf870272190850c4ce81f910e41538c5128fb6

@sholderbach

Copy link
Copy Markdown
Member

Thanks, let's give it a try :)

@sholderbach
sholderbach merged commit c5a14bb into nushell:main Feb 28, 2025
@github-actions github-actions Bot added this to the v0.103.0 milestone Feb 28, 2025
@sgvictorino
sgvictorino deleted the glob-interruption branch March 3, 2025 02:36
@fdncred fdncred added deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way A:signal-cancel Problems with how executions react to signals or attempts at cancellation (CTRL-C) labels Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:signal-cancel Problems with how executions react to signals or attempts at cancellation (CTRL-C) deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ls hangs and cannot be killed with ^C

3 participants