Refactor session cleanup logic to ensure single execution and improve resource management
- Replace direct cleanup calls in the Close method with a sync.Once mechanism to guarantee that cleanup runs only once. - Remove the cleanup method and integrate its functionality into the Close method, streamlining resource management. - Update the serve method to call Close for cleanup, enhancing clarity and consistency in session termination.
This commit is contained in:
parent
ae346435b9
commit
add0424576
2 changed files with 15 additions and 25 deletions
|
|
@ -24,23 +24,24 @@ func (s *Session) SetContext(ctx *AgentContext) {
|
||||||
|
|
||||||
// Close closes the session and cleans up resources
|
// Close closes the session and cleans up resources
|
||||||
func (s *Session) Close() error {
|
func (s *Session) Close() error {
|
||||||
if s.cancel != nil {
|
s.closeOnce.Do(func() {
|
||||||
s.cancel()
|
if s.cancel != nil {
|
||||||
}
|
s.cancel()
|
||||||
if s.Conn != nil {
|
}
|
||||||
s.Conn.Close()
|
if s.Conn != nil {
|
||||||
}
|
s.Conn.Close()
|
||||||
if s.Listener != nil {
|
}
|
||||||
s.Listener.Close()
|
if s.Listener != nil {
|
||||||
}
|
s.Listener.Close()
|
||||||
// Remove socket file
|
}
|
||||||
os.Remove(s.SocketPath)
|
os.Remove(s.SocketPath)
|
||||||
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// serve handles incoming connections
|
// serve handles incoming connections
|
||||||
func (s *Session) serve(ctx context.Context) {
|
func (s *Session) serve(ctx context.Context) {
|
||||||
defer s.cleanup()
|
defer s.Close()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
|
@ -49,10 +50,8 @@ func (s *Session) serve(ctx context.Context) {
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
// Accept connection with deadline to allow context cancellation check
|
|
||||||
conn, err := s.Listener.Accept()
|
conn, err := s.Listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Check if context was cancelled
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
|
|
@ -66,17 +65,6 @@ func (s *Session) serve(ctx context.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanup cleans up session resources
|
|
||||||
func (s *Session) cleanup() {
|
|
||||||
if s.Conn != nil {
|
|
||||||
s.Conn.Close()
|
|
||||||
}
|
|
||||||
if s.Listener != nil {
|
|
||||||
s.Listener.Close()
|
|
||||||
}
|
|
||||||
os.Remove(s.SocketPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
// handleConnection handles a single connection
|
// handleConnection handles a single connection
|
||||||
func (s *Session) handleConnection(ctx context.Context, conn net.Conn) {
|
func (s *Session) handleConnection(ctx context.Context, conn net.Conn) {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net"
|
"net"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Session represents an IPC session for a sandbox container
|
// Session represents an IPC session for a sandbox container
|
||||||
|
|
@ -15,6 +16,7 @@ type Session struct {
|
||||||
Context *AgentContext // Agent context
|
Context *AgentContext // Agent context
|
||||||
MCPTools map[string]*MCPTool // Authorized MCP tools
|
MCPTools map[string]*MCPTool // Authorized MCP tools
|
||||||
cancel context.CancelFunc // Cancel function for cleanup
|
cancel context.CancelFunc // Cancel function for cleanup
|
||||||
|
closeOnce sync.Once // Ensures cleanup runs exactly once
|
||||||
}
|
}
|
||||||
|
|
||||||
// AgentContext holds context information for the agent
|
// AgentContext holds context information for the agent
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue