Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[preview-tui] updated tree breaks preview file/dir list #1291

Closed
5 of 9 tasks
duffydack opened this issue Jan 21, 2022 · 18 comments · Fixed by #1307
Closed
5 of 9 tasks

[preview-tui] updated tree breaks preview file/dir list #1291

duffydack opened this issue Jan 21, 2022 · 18 comments · Fixed by #1307
Labels

Comments

@duffydack
Copy link

duffydack commented Jan 21, 2022

Environment details (Put x in the checkbox along with the information)

  • Operating System: Arch Linux
  • Desktop Environment: Sway
  • Terminal Emulator: foot
  • Shell: zsh
  • Custom desktop opener (if applicable):
  • Program options used:
  • Configuration options set:
  • Plugins are installed
  • Issue exists on nnn master

Arch Linux with tree 2.0.1-2, using preview-tui script, now file/dir listings don't display correctly anymore. See [1]
Running the same tree command that preview-tui uses, in a shell, it still works. See [2]
Downgrading to 1.8.0-2 fixes, as expected.

[1] https://0x0.st/ooQa.png
[2] https://0x0.st/ooQB.png

@duffydack duffydack added the bug label Jan 21, 2022
@luukvbaal
Copy link
Collaborator

Seems like tree's -J flag is active in your preview-tui.

It's not there on master and I have no idea how it could be caused by updating, -J flag is the same for 1.8 -> 2.0.

The issue is also not present for me on 2.0.1-2, also on Arch. Please debug yourself.

@duffydack
Copy link
Author

Didn't even think to try another terminal. Tried kitty, it's fine, so it's foot that's the issue. /sigh
I'll look into it and report if I find anything, in case anyone searches for same issue.

@badxnach
Copy link

This issue seems not to be limited to preview-tui.
I see the same as in duffydack's Picture 1 when I run "tree" within a shell opened with nnn by pressing ! or ^]

tree works fine in both xterm and urxvt without nnn

@dnkl
Copy link
Contributor

dnkl commented Feb 12, 2022

tree automatically enables JSON output if the file descriptor STDDATA_FILENO exists.

This is, as far as I know, not a standardized file descriptor (like stdin/stdout/stderr), but rather something made up by tree. It is defined (in the tree source code) to 3. treeonly checks for this file descriptor, it does not verify its type or perform any other sanity checks.

In my case, tree-under-zsh-under-nnn has an inotify descriptor opened at FD 3. As far as I can tell, it's nnn that has opened it, and it is then being inherited from nnn to zsh, and finally to tree.

The following wrapper script shows that FD 3 is the cause of all this (it closes FD 3 and then exec's tree):

#!/usr/bin/bash
exec 3>&-
exec tree

@dnkl
Copy link
Contributor

dnkl commented Feb 12, 2022

nnn/src/nnn.c

Line 8635 in 23b54ba

inotify_fd = inotify_init1(IN_NONBLOCK);

Should probably set IN_CLOEXEC.

@N-R-K
Copy link
Collaborator

N-R-K commented Feb 12, 2022

tree automatically enables JSON output if the file descriptor STDDATA_FILENO exists.

This is, as far as I know, not a standardized file descriptor (like stdin/stdout/stderr), but rather something made up by tree. It is defined (in the tree source code) to 3. treeonly checks for this file descriptor, it does not verify its type or perform any other sanity checks.

I don't use tree nor am I familiar with it's codebase, but what you're describing sounds like it is something that should be addressed by tree's upstream rather than us trying to hack around it.

@dnkl
Copy link
Contributor

dnkl commented Feb 12, 2022

I certainly don't agree with tree's implementation.

But with that said, you never exec other programs with file-descriptors other than stdin/stdout/stderr open. Unless it's intended. I.e. you are passing an FD to the child on purpose. But this; an inotify FD used by nnn itself, should not be passed to the shell.

Closing your own file-descriptors before exec:ing a child is not "hacking around it". It's what you're supposed to do.

@N-R-K
Copy link
Collaborator

N-R-K commented Feb 12, 2022

@dnkl I didn't pay attention to your other post and was under the impression you were proposing using that bash script as a solution. Sorry for the confusion.

@dnkl
Copy link
Contributor

dnkl commented Feb 12, 2022

No worries, and I hope I didn't come off as being rude. The bash script was only meant as a way to show/prove that the leaked FD is the cause, and I could have been more clear about that.

@badxnach
Copy link

Thank you very much for checking this in such depth, dnkl.
Can we do something about this issue? I use tree quite often to make directory listings of external hdds.

@dnkl
Copy link
Contributor

dnkl commented Feb 12, 2022

imho, nnn needs to be patched.

Not sure if it leaks other file descriptors as well; one can either inspect the nnn source code, and ensure all opened file descriptors have CLOEXEC set. Or, nnn can loop, and just "try" to close all file descriptors in, say, the range 3-1024 after having forked, but before exec:ing a process.

@N-R-K
Copy link
Collaborator

N-R-K commented Feb 12, 2022

Making thousands of syscall in a loop sounds like a really bad idea. Using CLOEXEC everywhere is the probably the proper solution here.

I'm at the end of my awake hours though. We can pick this up tomorrow, or someone else wants to take a crack then feel free to do so.

@jarun

@N-R-K N-R-K reopened this Feb 12, 2022
@N-R-K
Copy link
Collaborator

N-R-K commented Feb 15, 2022

@badxnach @dnkl does this fix the issue with tree?

diff --git a/src/nnn.c b/src/nnn.c
index a0fafe9..a6491bd 100644
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -8629,7 +8629,7 @@ int main(int argc, char *argv[])
 
 #ifdef LINUX_INOTIFY
 	/* Initialize inotify */
-	inotify_fd = inotify_init1(IN_NONBLOCK);
+	inotify_fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
 	if (inotify_fd < 0) {
 		xerror();
 		return EXIT_FAILURE;

Skimmed through most of the open() calls and they appear to be short lived, don't think they should leak to the plugins.


Also just out of curiosity, any clue on what's going on here with kitty? @dnkl

Tried kitty, it's fine, so it's foot that's the issue.

@badxnach
Copy link

Can cofirm that this fixes the issue on my end.
I checked tree, preview-tui and two of my own plugins. All work as they should.
Thank you very much, @N-R-K and @dnkl for investigating and solving this.

N-R-K added a commit to N-R-K/nnn that referenced this issue Feb 15, 2022
Closes: jarun#1291

Co-authored-by: Daniel Eklöf <daniel@ekloef.se>
@N-R-K N-R-K linked a pull request Feb 15, 2022 that will close this issue
@dnkl
Copy link
Contributor

dnkl commented Feb 15, 2022 via email

@dnkl
Copy link
Contributor

dnkl commented Feb 15, 2022 via email

@duffydack
Copy link
Author

duffydack commented Feb 18, 2022

on arch, tree 2.0.2-1 fixes the issue. tree changelog,

Version 2.0.2 (02/16/2022)
Okay, apparently the stddata addition is causing havoc (who knew how many scripts just haphazardly hand programs random file descriptors, that's surely not a problem.) Going forward the stddata option will only work if the environment variable STDDATA_FD is present or set to the descriptor to produce the JSON output on.

@N-R-K
Copy link
Collaborator

N-R-K commented Feb 19, 2022

Going forward the stddata option will only work if the environment variable STDDATA_FD is present or set to the descriptor to produce the JSON output on.

That's definitely the right move. 😄

@N-R-K N-R-K mentioned this issue Mar 4, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Mar 22, 2022
cgxxv pushed a commit to cgxxv/nnn that referenced this issue May 3, 2022
Closes: jarun#1291

Co-authored-by: Daniel Eklöf <daniel@ekloef.se>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants