# How to cancel the context of custom middleware plugin?

**URL:** <https://community.traefik.io/t/how-to-cancel-the-context-of-custom-middleware-plugin/12099>\
**Category:** Traefik v2\
**Tags:** plugin, middleware\
**Created:** [October 14, 2021, 6:49am UTC](https://community.traefik.io/t/how-to-cancel-the-context-of-custom-middleware-plugin/12099 "2021-10-14T06:49:58Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![yuusaku-t](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/yuusaku-t/32/4381_2.png) [@yuusaku-t](https://community.traefik.io/u/yuusaku-t)\
**Post date:** [October 14, 2021, 6:49am UTC](https://community.traefik.io/t/how-to-cancel-the-context-of-custom-middleware-plugin/12099/1 "2021-10-14T06:49:58Z")

</div>

I want to cancel the go-routines invoked by my custom middleware as follows. However, these go-routines are continued to run even after the middleware is no longer in use. Is there any way to cancel the go-routine?

```go
// New created a new Demo plugin.
func New(ctx context.Context, next http.Handler, conf *Config, name string) (http.Handler, error) {
	go func() {
		for {
			select {
			case <-ctx.Done():
				// ctx should be canceled when the middleware is no longer in use
				// in my expectation. However, it is not cancelled.
				return
			}
		}
	}()
	return &Demo{
		next: next,
	}, nil
}

```
