Support reading multiple bytes in escapeProxy#39728
Conversation
7f9f07f to
902392d
Compare
thaJeztah
left a comment
There was a problem hiding this comment.
Thanks! Did a cursory review, and left some comments/suggestions
pkg/term/proxy.go
Outdated
There was a problem hiding this comment.
Wondering if it'd be clearer to return early here (possibly at the cost of some slight duplication)
There was a problem hiding this comment.
Here I think it could be replaced with:
n = i + 1 - r.escapeKeyPos
if n < 0 {
n = 0
}
return n, EscapeError{}Let me know if that looks better and I'll try it out.
There was a problem hiding this comment.
yes please! +1 to "return early"
There was a problem hiding this comment.
may be in a follow up PR is fine.
pkg/term/proxy.go
Outdated
There was a problem hiding this comment.
Not a huge fan of named output variables; it's easy to inadvertently mask them and introduce bugs; perhaps we can slightly refactor the code to not require them
There was a problem hiding this comment.
I'd have to make variable declarations at the beginning of the function, this wouldn't look better IMO, I don't see an alternative with the current implementation.
There was a problem hiding this comment.
Hmm, 🤞 that we won't have to touch this again! so ok for named output variable here
902392d to
c650123
Compare
|
Sorry for the delay; #39800 was merged, so you can rebase this PR 🤗 |
c650123 to
9b529fe
Compare
|
@thaJeztah : awesome 🎉 it's rebased now! |
|
The build failed: I'll try to re-run. |
9b529fe to
b327b99
Compare
b327b99 to
b150078
Compare
|
@thaJeztah : I've rebased since #39887 was merged and CI passed 🚀 |
|
@thaJeztah @runcom : I've rebased with the PR improving the tests and CI is now passing, is there anything else missing here? |
|
@thaJeztah : is this PR still on your radar? |
thaJeztah
left a comment
There was a problem hiding this comment.
ah, whoops, no it wasn't 😅
LGTM
|
ping @vdemeester PTAL, I think you're familiar with this area |
|
@vdemeester any news on this one? 👼 |
|
Hey @thaJeztah @vdemeester, can you help with this one? It's been sitting here for a while. |
pkg/term/proxy_test.go
Outdated
There was a problem hiding this comment.
thanks for the additional tests. helps understand the problem better.
|
LGTM thanks @bamarni |
|
@dims : so is it good to go, or should I apply your comment about early return here? |
|
@bamarni if you can quickly make the change for the early return, that would be good. I am fine if you want to do it in a follow up PR as this has been stuck forever |
|
If we are going to make this change, I'd like to simplify this down. I kinda started messing with this, it's not 100% yet, but PTAL: func (r *escapeProxy) Read(p []byte) (nr int, err error) {
if len(r.escapeKeys) == 0 {
// This causes tests to fail b/c they are expecting an error in this case.
// I'm not sure that's correct... but in any case this is a place holder to prevent test panics
return r.r.Read(p)
}
for i := 0; i < len(p); i++ {
b, err := r.r.ReadByte()
if err != nil {
return nr, err
}
p[nr] = b
if b != r.escapeKeys[r.escapeKeyPos] {
r.escapeKeyPos = 0
nr++
continue
}
r.escapeKeyPos++
if r.escapeKeyPos == len(r.escapeKeys) {
return nr, EscapeError{}
}
}
return nr, nil
} |
Currently, the escapeProxy works under the assumption that the underlying reader will always return 1 byte at a time. Even though this is usually true, it is not always the case, for example when using a pty and writing multiple bytes to the master before flushing it. In such cases the proxy reader doesn't work properly. For example with an escape sequence being `ctrl-p,ctrl-q`, when the underlying reader returns `ctrl-p,ctrl-q` at once, the escape sequence isn't detected. This updates the reader to support this use-case and adds unit tests. Signed-off-by: Bilal Amarni <[email protected]>
|
@cpuguy83 : Ah I see, so the bufio.Reader would read 4096 by default and keep it in memory, then we'd iterate one byte at a time. |
|
@cpuguy83 : I've looked at your snippet:
It would require additional logic to fix these 2 (especially the 2nd point), and eventually I'm not sure that the resulting code would be necessarily simpler. Let me know what you think. |
|
@cpuguy83 PTAL |
cpuguy83
left a comment
There was a problem hiding this comment.
LGTM
Should we move this to github.com/moby/term?
|
LGTM +1 to move this to moby/term |
There used to be a bug in the Moby terminal escape proxy: moby/moby#39728 Since it is now fixed upstream, we can remove the patched version from the codebase. Additionally, this narrows down the Moby dependency to moby/term instead of the broad moby/moby package.
There used to be a bug in the Moby terminal escape proxy: moby/moby#39728 Since it is now fixed upstream, we can remove the patched version from the codebase. Additionally, this narrows down the Moby dependency to moby/term instead of the broad moby/moby package.
There used to be a bug in the Moby terminal escape proxy: moby/moby#39728 Since it is now fixed upstream, we can remove the patched version from the codebase. Additionally, this narrows down the Moby dependency to moby/term instead of the broad moby/moby package.
Currently, the escapeProxy works under the assumption that the
underlying reader will always return 1 byte at a time. Even though this
is usually true, it is not always the case, for example when using a pty
and writing multiple bytes to the master before flushing it.
In such cases the proxy reader doesn't work properly. For example with
an escape sequence being
ctrl-p,ctrl-q, when the underlying readerreturns
ctrl-p,ctrl-qat once, the escape sequence isn't detected.A second issue occurs when there is a partial escape currently being
read, if on the next read the len of the passed buffer in argument is
not big enough, it will cause a panic. For example, when the escape sequence
is
ctrl-p,ctrl-q, and the caller is passing a []byte of len 1 as argument to read.And they press
ctrl-p, then something else thanctrl-q.This updates the reader to support this use-cases and adds unit tests.
I'd still like to test more cases to make sure it's working fine, but I'm first
opening the PR to see if it is an acceptable patch.