dnkl/foot
41
2.0k
Fork
You've already forked foot
243

Possible race condition when using "sudo -e" or "sudoedit" #1743

Closed
opened 2024年06月21日 03:16:21 +02:00 by rhask · 14 comments

Foot Version

foot version: 1.17.2 +pgo +ime +graphemes -assertions +ucurl

TERM environment variable

foot

Compositor Version

sway version 1.9

Description of Bug and Steps to Reproduce

I have been trying to track down the source of this bug for quite some time. When using foot to run vim (latest version compiled from source) specifically via sudoedit or sudo -e where EDITOR=vim, sometimes Vim receives erroneous inputs immediately upon launching the program. Specifically, it appears to execute a 'normal mode paste' ("P") and inserts a number in the first position of the second line of the file (at the very least).

This issue does not occur every time sudoedit is used and I cannot reproduce when running via strace (which inadvertently introduces delay to Vim's startup). It only occurs with foot and not when using tmux (tested on alacritty as well).

This sequence in particular...
^[[?12l^[[?25h^[[2;1H▽^[[6n^[[2;1H ^[[3;1H^[Pzz^[\^[[0%m^[[6n^[[3;1H
... appears early in Vim's initialization, and I hasten to wonder whether it could be related/misinterpreted based on the environment.

I am not terribly familiar with how foot works, but perhaps this may assist someone in discovering why this may occur. I'm using the terminfo foot provides. I'll continue to try and uncover where this bug is coming from, but I first noticed it many months ago and I'm sure it has remained between versions of foot. Various forms of debugging haven't revealed much as there is no 'error,' but unpredictable behavior due to some confounding variable when using sudo -e.

Relevant logs, stacktraces, etc.

No response

### Foot Version foot version: 1.17.2 +pgo +ime +graphemes -assertions +ucurl ### TERM environment variable foot ### Compositor Version sway version 1.9 ### Description of Bug and Steps to Reproduce I have been trying to track down the source of this bug for quite some time. When using foot to run `vim` (latest version compiled from source) _specifically_ via `sudoedit` or `sudo -e` where `EDITOR=vim`, _**sometimes**_ Vim receives erroneous inputs immediately upon launching the program. Specifically, it appears to execute a 'normal mode paste' ("P") and inserts a number in the first position of the second line of the file (at the very least). This issue does not occur every time sudoedit is used and I cannot reproduce when running via `strace` (which inadvertently introduces delay to Vim's startup). It only occurs with foot and not when using tmux (tested on alacritty as well). This sequence in particular... ```^[[?12l^[[?25h^[[2;1H▽^[[6n^[[2;1H ^[[3;1H^[Pzz^[\^[[0%m^[[6n^[[3;1H``` ... appears early in Vim's initialization, and I hasten to wonder whether it could be related/misinterpreted based on the environment. I am not terribly familiar with how foot works, but perhaps this may assist someone in discovering why this may occur. I'm using the terminfo foot provides. I'll continue to try and uncover where this bug is coming from, but I first noticed it many months ago and I'm sure it has remained between versions of foot. Various forms of debugging haven't revealed much as there is no 'error,' but unpredictable behavior due to some confounding variable when using `sudo -e`. ### Relevant logs, stacktraces, etc. _No response_
Collaborator
Copy link

sometimes Vim receives erroneous inputs immediately upon launching the program
...
Specifically, it appears to execute a 'normal mode paste' ("P") and inserts a number in the first position of the second line of the file

The first thing that comes to mind when I read that is that it sounds a lot like a mishandled query or query response. (削除) Especially since the sequence you mention includes the character P. (削除ここまで)

This sequence in particular...
^[[?12l^[[?25h^[[2;1H▽^[[6n^[[2;1H ^[[3;1H^[Pzz^[\^[[0%m^[[6n^[[3;1H

How did you observe that sequence? By intercepting the terminal I/O?

> sometimes Vim receives erroneous inputs immediately upon launching the program > ... > Specifically, it appears to execute a 'normal mode paste' ("P") and inserts a number in the first position of the second line of the file The first thing that comes to mind when I read that is that it sounds a lot like a mishandled query or query response. ~~Especially since the sequence you mention includes the character `P`.~~ >This sequence in particular... >`^[[?12l^[[?25h^[[2;1H▽^[[6n^[[2;1H ^[[3;1H^[Pzz^[\^[[0%m^[[6n^[[3;1H` How did you observe that sequence? By intercepting the terminal I/O?
Collaborator
Copy link

Especially since the sequence you mention includes the character P

Looking a little closer at that sequence, this part of my previous reply can probably be disregarded. Rogue input at startup still sounds reminiscent of query/response parsing issues to me, but that sequence doesn't look like a likely culprit.

FWIW I tried to reproduce the issue in vim 9.1, by running the following (10 times):

SUDO_EDITOR=vim sudoedit /etc/xyz

...and nothing out of the ordinary happens.

... sometimes Vim receives erroneous inputs

How often is "sometimes"?

> Especially since the sequence you mention includes the character `P` Looking a little closer at that sequence, this part of my previous reply can probably be disregarded. Rogue input at startup still sounds reminiscent of query/response parsing issues to me, but that sequence doesn't look like a likely culprit. FWIW I tried to reproduce the issue in vim 9.1, by running the following (10 times): ```sh SUDO_EDITOR=vim sudoedit /etc/xyz ``` ...and nothing out of the ordinary happens. > ... _**sometimes**_ Vim receives erroneous inputs How often is "sometimes"?
Collaborator
Copy link

... that sequence doesn't look like a likely culprit

Although it does look somewhat nonsensical. Specifically this part:

^[Pzz^[\^[[0%m

> ... that sequence doesn't look like a likely culprit Although it does look somewhat nonsensical. Specifically this part: > `^[Pzz^[\^[[0%m`
Author
Copy link

@craigbarnes I observed the sequence using script -f <log_file> then doing what I wrote below. The video I've attached (28 seconds) shows the bug (and fortunately, I was able to reproduce it on the second attempt).

Here are the steps:

  1. I opened vim with just vim (not sudoedit).
  2. I typed some text and deleted all of it with ggVGd (thus it goes into some undo file) then closed the buffer with :q!.
  3. I opened a file owned by root with sudo -e and the bug fails to manifest on the first attempt.
  4. I opened a file owned by root with sudo -e again and immediately upon launch, the previously deleted text is inserted along with a number (in this case, "4," but sometimes "2" or "6") in the second row, position 1.
  5. (Not necessary) I pushed u twice to show Vim received what appears to be two inputs.

Edit: I realize the vim command line isn't visible, but the only other command not shown is :q to exit the file and try again.

@craigbarnes I observed the sequence using `script -f <log_file>` then doing what I wrote below. The video I've attached (28 seconds) shows the bug (and fortunately, I was able to reproduce it on the second attempt). Here are the steps: 1. I opened vim with just `vim` (not `sudoedit`). 2. I typed some text and deleted all of it with `ggVGd` (thus it goes into some undo file) then closed the buffer with `:q!`. 3. I opened a file owned by root with `sudo -e` and the bug _fails to manifest on the first attempt_. 4. I opened a file owned by root with `sudo -e` again and immediately upon launch, the previously deleted text is inserted along with a number (in this case, "4," but sometimes "2" or "6") in the second row, position 1. 5. (Not necessary) I pushed `u` twice to show Vim received what appears to be two inputs. Edit: I realize the vim command line isn't visible, but the only other command not shown is `:q` to exit the file and try again.
Author
Copy link

For clarity, that sequence appears regardless of whether the bug manifests... my curiosity is whether it's possible for Vim to misinterpret a query/response sequence as command inputs under "some circumstance" that happens occasionally (and I'm not sure what it would be, but the symptoms look like a race condition — potentially created by 'sudoedit' needing to 'set up' the environment?).

Edit: Sometimes is ~15% of the time if I had to guess... I could produce a real figure I suppose, but that's my sense from experience.

For clarity, that sequence appears regardless of whether the bug manifests... my curiosity is whether it's possible for Vim to misinterpret a query/response sequence as command inputs under "some circumstance" that happens occasionally (and I'm not sure what it would be, but the symptoms look like a race condition — potentially created by 'sudoedit' needing to 'set up' the environment?). Edit: Sometimes is ~15% of the time if I had to guess... I could produce a real figure I suppose, but that's my sense from experience.
Owner
Copy link

Could perhaps be a duplicate of #1734?

Could perhaps be a duplicate of https://codeberg.org/dnkl/foot/issues/1734?
Author
Copy link

I built from the master branch and tested; still occurs.

I built from the master branch and tested; still occurs.
Author
Copy link

Here's where it seems that Pzz bit is coming from... this could be a red herring; I'm trying to cleverly deduce where a "P" input may have come from, but this is out of my reach. The obvious question is why this—if it is responsible—only causes a problem sometimes with sudoedit.

https://github.com/vim/vim/blob/master/src/term.c#L4144

Here's where it seems that Pzz bit is coming from... this could be a red herring; I'm trying to cleverly deduce where a "P" input may have come from, but this is out of my reach. The obvious question is why this—_if_ it is responsible—only causes a problem sometimes with sudoedit. https://github.com/vim/vim/blob/master/src/term.c#L4144
Author
Copy link

Oh, I should have read the comment... this seems relevant:

	// We move the cursor to (2, 0), print a test sequence and then query
	// the current cursor position. If the terminal properly handles
	// unknown DCS string and CSI sequence with intermediate byte, the test
	// sequence is ignored and the cursor does not move. If the terminal
	// handles test sequence incorrectly, a garbage string is displayed and
	// the cursor does move.
Oh, I should have read the comment... this seems relevant: ``` // We move the cursor to (2, 0), print a test sequence and then query // the current cursor position. If the terminal properly handles // unknown DCS string and CSI sequence with intermediate byte, the test // sequence is ignored and the cursor does not move. If the terminal // handles test sequence incorrectly, a garbage string is displayed and // the cursor does move. ```
Owner
Copy link

Maybe some weird interaction between vim, sudo and #453?

Maybe some weird interaction between vim, sudo and https://codeberg.org/dnkl/foot/issues/453?
Author
Copy link

(削除) It seems likely. (削除ここまで) (e: further exploration below) If I understand correctly, the issue lies with sudo and other programs (besides vim) launched with sudo could be affected.

~~It seems likely.~~ (e: further exploration below) If I understand correctly, the issue lies with sudo and other programs (besides vim) launched with sudo could be affected.
Author
Copy link

I tried using waitwinch, but the issue still manifests with waitwinch sudoedit <file>. If #453 is related only to new windows launched in Sway, then this is unlikely to be related.

I tried using [waitwinch](https://codeberg.org/craigbarnes/waitwinch), but the issue still manifests with `waitwinch sudoedit <file>`. If #453 is related only to new windows launched in Sway, then this is unlikely to be related.
Author
Copy link

I narrowed down the culprit to ttimeoutlen=0 (which is mentioned here https://codeberg.org/dnkl/foot/wiki#enabling-disabling-ime-when-entering-leaving-insert-mode-in-vim). It seems that setting allows exiting insert mode in tmux without delay, but causes an issue without tmux and when TERM=foot if vim is launched via sudoedit.

I narrowed down the culprit to `ttimeoutlen=0` (which is mentioned here https://codeberg.org/dnkl/foot/wiki#enabling-disabling-ime-when-entering-leaving-insert-mode-in-vim). It seems that setting allows exiting insert mode in tmux without delay, but causes an issue without tmux and when TERM=foot if vim is launched via sudoedit.
Author
Copy link

I'll add for anyone who may come across this in the future, this Vim configuration will solve the issue:

if $TERM == "tmux-256color"
 set ttimeoutlen=0
endif
I'll add for anyone who may come across this in the future, this Vim configuration will solve the issue: ```vim if $TERM == "tmux-256color" set ttimeoutlen=0 endif ```
Sign in to join this conversation.
No Branch/Tag specified
master
osc-5522
sixel-heap-buffer-overflow
releases/1.27
releases/1.26
releases/1.25
releases/1.24
multi-cursor
releases/1.23
pixman-16f-2
releases/1.22
releases/1.21
releases/1.20
releases/1.19
releases/1.18
releases/1.17
releases/1.16
releases/1.15
releases/1.14
releases/1.13
releases/1.12
releases/1.11
releases/1.10
releases/1.9
releases/1.8
releases/1.7
releases/1.6
releases/1.5
releases/1.4
releases/1.3
releases/1.2
releases/1.1
releases/1.0
1.27.0
1.26.1
1.26.0
1.25.0
1.24.0
1.23.1
1.23.0
1.22.3
1.22.2
1.22.1
1.22.0
1.21.0
1.20.2
1.20.1
1.20.0
1.19.0
1.18.1
1.18.0
1.17.2
1.17.1
1.17.0
1.16.2
1.16.1
1.16.0
1.15.3
1.15.2
1.15.1
1.15.0
1.14.0
1.13.1
1.13.0
1.12.1
1.12.0
1.11.0
1.10.3
1.10.2
1.10.1
1.10.0
1.9.2
1.9.1
1.9.0
1.8.2
1.8.1
1.8.0
1.7.2
1.7.1
1.7.0
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.4
1.4.3
1.4.2
1.4.1
1.4.0
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.0
1.0.0
0.9.0
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
3 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dnkl/foot#1743
Reference in a new issue
dnkl/foot
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?