@@ -481,10 +481,11 @@ type Server struct {
481481 wg sync.WaitGroup
482482}
483483
484- // Serve serves the FUSE connection by making calls to the methods
485- // of fs and the Nodes and Handles it makes available. It returns only
486- // when the connection has been closed or an unexpected error occurs.
487- func (s * Server ) Serve (fs FS ) error {
484+ // ServeContext serves the FUSE connection by making calls to the methods
485+ // of fs and the Nodes and Handles it makes available. It returns when the
486+ // context is closed, when the connection has been closed, or an unexpected
487+ // error occurs.
488+ func (s * Server ) ServeContext (ctx context.Context , fs FS ) error {
488489 defer s .wg .Wait () // Wait for worker goroutines to complete before return
489490
490491 s .fs = fs
@@ -507,22 +508,48 @@ func (s *Server) Serve(fs FS) error {
507508 })
508509 s .handle = append (s .handle , nil )
509510
510- for {
511- req , err := s .conn .ReadRequest ()
512- if err != nil {
513- if err == io .EOF {
514- break
511+ retErr := make (chan error )
512+
513+ go func () {
514+ for {
515+ req , err := s .conn .ReadRequest ()
516+ if err != nil {
517+ if err == io .EOF {
518+ break
519+ }
520+ retErr <- err
521+ return
515522 }
516- return err
523+
524+ s .wg .Add (1 )
525+ go func () {
526+ defer s .wg .Done ()
527+ s .serve (ctx , req )
528+ }()
517529 }
530+ retErr <- nil
531+ }()
518532
519- s . wg . Add ( 1 )
520- go func () {
521- defer s . wg . Done ()
522- s . serve ( req )
523- }()
533+ select {
534+ case err := <- retErr :
535+ return err
536+ case <- ctx . Done ():
537+ return nil
524538 }
525- return nil
539+ }
540+
541+ // Serve serves the FUSE connection by making calls to the methods
542+ // of fs and the Nodes and Handles it makes available. It returns only
543+ // when the connection has been closed or an unexpected error occurs.
544+ func (s * Server ) Serve (fs FS ) error {
545+ return s .ServeContext (context .Background (), fs )
546+ }
547+
548+ // ServeContext serves a FUSE connection with the default settings. See
549+ // Server.ServeContext.
550+ func ServeContext (ctx context.Context , c * fuse.Conn , fs FS ) error {
551+ server := New (c , nil )
552+ return server .ServeContext (ctx , fs )
526553}
527554
528555// Serve serves a FUSE connection with the default settings. See
@@ -915,8 +942,8 @@ func (m *logDuplicateRequestID) String() string {
915942 return fmt .Sprintf ("Duplicate request: new %v, old %v" , m .New , m .Old )
916943}
917944
918- func (c * Server ) serve (r fuse.Request ) {
919- ctx , cancel := context .WithCancel (context . Background () )
945+ func (c * Server ) serve (originContext context. Context , r fuse.Request ) {
946+ ctx , cancel := context .WithCancel (originContext )
920947 defer cancel ()
921948 parentCtx := ctx
922949 if c .context != nil {
0 commit comments