Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbb744e8

Browse files
committed
chore: update X11 forward session usage when there is a connection
1 parentfa90888 commitbb744e8

File tree

2 files changed

+53
-8
lines changed

2 files changed

+53
-8
lines changed

‎agent/agentssh/x11.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ func (x *x11Forwarder) listenForConnections(ctx context.Context, session *x11Ses
161161
x.logger.Warn(ctx,"failed to accept X11 connection",slog.Error(err))
162162
return
163163
}
164+
165+
// Update session usage time since a new X11 connection was forwarded.
166+
x.mu.Lock()
167+
session.usedAt=time.Now()
168+
x.mu.Unlock()
164169
ifx11.SingleConnection {
165170
x.logger.Debug(ctx,"single connection requested, closing X11 listener")
166171
x.cleanSession(session)

‎agent/agentssh/x11_test.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,31 @@ func TestServer_X11_EvictionLRU(t *testing.T) {
234234
})
235235
}
236236

237-
// Create one more session which should evict the first (LRU) session and
238-
// therefore reuse the very first display/port.
237+
// Connect X11 forwarding to the first session. This is used to test that
238+
// connecting counts as a use of the display.
239+
x11Chans:=c.HandleChannelOpen("x11")
240+
payload:="hello world"
241+
gofunc() {
242+
conn,err:=inproc.Dial(ctx,testutil.NewAddr("tcp",fmt.Sprintf("localhost:%d",agentssh.X11StartPort+agentssh.X11DefaultDisplayOffset)))
243+
assert.NoError(t,err)
244+
_,err=conn.Write([]byte(payload))
245+
assert.NoError(t,err)
246+
_=conn.Close()
247+
}()
248+
249+
x11:=testutil.RequireReceive(ctx,t,x11Chans)
250+
ch,reqs,err:=x11.Accept()
251+
require.NoError(t,err)
252+
gogossh.DiscardRequests(reqs)
253+
got:=make([]byte,len(payload))
254+
_,err=ch.Read(got)
255+
require.NoError(t,err)
256+
assert.Equal(t,payload,string(got))
257+
_=ch.Close()
258+
259+
// Create one more session which should evict a session and reuse the display.
260+
// The first session was used to connect X11 forwarding, so it should not be evicted.
261+
// Therefore, the second session should be evicted and its display reused.
239262
extraSess,err:=c.NewSession()
240263
require.NoError(t,err)
241264

@@ -273,17 +296,34 @@ func TestServer_X11_EvictionLRU(t *testing.T) {
273296
require.NoError(t,sc.Err())
274297
}
275298

276-
// The display number should have wrapped around to the starting value.
277-
assert.Equal(t,agentssh.X11DefaultDisplayOffset,newDisplayNumber,"expected display number to be reused after eviction")
299+
// The display number reused should correspond to the SECOND session (display offset 12)
300+
expectedDisplay:=agentssh.X11DefaultDisplayOffset+2// +1 was blocked port
301+
assert.Equal(t,expectedDisplay,newDisplayNumber,"second session should have been evicted and its display reused")
278302

279-
// validate that the first session was torn down.
280-
_,err=sessions[0].stdin.Write([]byte("echo DISPLAY=$DISPLAY\n"))
303+
// First session should still be alive: send cmd and read output.
304+
msgFirst:="still-alive"
305+
_,err=sessions[0].stdin.Write([]byte("echo "+msgFirst+"\n"))
306+
require.NoError(t,err)
307+
forsessions[0].scanner.Scan() {
308+
line:=strings.TrimSpace(sessions[0].scanner.Text())
309+
ifstrings.Contains(line,msgFirst) {
310+
break
311+
}
312+
}
313+
require.NoError(t,sessions[0].scanner.Err())
314+
315+
// Second session should now be closed.
316+
_,err=sessions[1].stdin.Write([]byte("echo dead\n"))
281317
require.ErrorIs(t,err,io.EOF)
282-
err=sessions[0].sess.Wait()
318+
err=sessions[1].sess.Wait()
283319
require.Error(t,err)
284320

285321
// Cleanup.
286-
for_,sh:=rangesessions[1:] {
322+
fori,sh:=rangesessions {
323+
ifi==1 {
324+
// already closed
325+
continue
326+
}
287327
err=sh.stdin.Close()
288328
require.NoError(t,err)
289329
err=sh.sess.Wait()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp