@@ -72,10 +72,11 @@ type PacketForwarder[T io.Closer] interface {
7272}
7373
7474// FileAdaptorForInterface is a generic function that returns a file for the given [Network].
75- // The returned file is used as a file descriptor for network devices in QEMU or Virtualization frameworks.
75+ // The returned file is used as a file descriptor for network devices in QEMU, krunkit, or Virtualization frameworks.
7676// - Invoke the returned function in a separate goroutine to start packet forwarding between the vmnet interface and the file.
7777// - The context can be used to stop the goroutines and the interface.
7878// - The returned error channel can be used to receive errors from the goroutines.
79+ // - The connection closure is reported as [io.EOF] error or [syscall.ECONNRESET] error in the error channel.
7980func FileAdaptorForInterface [T PacketForwarder [U ], U io.Closer ](ctx context.Context , iface * Interface , opts ... Sockopt ) (file * os.File , start func (), errCh <- chan error , err error ) {
8081 var factory T
8182 forwarder := factory .New ()
@@ -106,6 +107,10 @@ func FileAdaptorForInterface[T PacketForwarder[U], U io.Closer](ctx context.Cont
106107 errChRW := make (chan error , 10 )
107108 reportError := func (err error , message string ) {
108109 if err != nil {
110+ if strings .Contains (err .Error (), "use of closed network connection" ) {
111+ // Silently ignore the error caused by connection closure
112+ return
113+ }
109114 errChRW <- fmt .Errorf ("%s: %w" , message , err )
110115 }
111116 }
@@ -144,12 +149,10 @@ func FileAdaptorForInterface[T PacketForwarder[U], U io.Closer](ctx context.Cont
144149 // Read all available packets in a loop.
145150 for {
146151 // Read packets from the connection to writeDescs
152+ // It won't return until at least one packet is read or connection is closed.
153+ // Remote closure may be detected as io.EOF on stream connection.
147154 packetCount , err := forwarder .ReadPacketsFromConn (conn )
148155 if err != nil {
149- if strings .Contains (err .Error (), "use of closed network connection" ) {
150- // Normal closure
151- break
152- }
153156 reportError (err , "forwarder.ReadPacketsFromConn failed" )
154157 break
155158 }
0 commit comments