package turms

import (
	"golang.org/x/net/context"
)

// A Chain is a chain of Handlers. It implements a Handler.
// Chains can be nested.
type Chain []Handler

// Handle calls each chain element Handle function subsequently.
func (c *Chain) Handle(ctx context.Context, conn Conn, msg Message) context.Context {
	chainCtx := ctx
	for i := range *c {
		rCtx := (*c)[i].Handle(chainCtx, conn, msg)
		if rCtx != nil {
			chainCtx = rCtx
		}
		select {
		case <-chainCtx.Done():
			return ctx
		default:
		}
	}
	return ctx
}

package turms

import (
	"golang.org/x/net/context"
)

// A Chain is a chain of Handlers. It implements a Handler.
// Chains can be nested.
type Chain []Handler

// Handle calls each chain element Handle function subsequently.
func (c *Chain) Handle(ctx context.Context, conn Conn, msg Message) context.Context {
	chainCtx := ctx
	for i := range *c {
		chainCtx = (*c)[i].Handle(chainCtx, conn, msg)
		if chainCtx == nil {
			chainCtx = ctx
		}
		select {
		case <-chainCtx.Done():
			return ctx
		default:
		}
	}
	return ctx
}

--- 
+++ 
@@ -12,9 +12,9 @@
 func (c *Chain) Handle(ctx context.Context, conn Conn, msg Message) context.Context {
 	chainCtx := ctx
 	for i := range *c {
-		chainCtx = (*c)[i].Handle(chainCtx, conn, msg)
-		if chainCtx == nil {
-			chainCtx = ctx
+		rCtx := (*c)[i].Handle(chainCtx, conn, msg)
+		if rCtx != nil {
+			chainCtx = rCtx
 		}
 		select {
 		case <-chainCtx.Done():
Use the previous context instead of the root context in Chain.Handle when a call to Handle returns a nil value.
