celestix/gotgproto

`fatal error: stack overflow` on ext.Context context.WithValue

Closed this issue · 2 comments

I'm trying to add contextual logging using zerolog and context.Context.
I'm adding handler to dispatcher:

dispatcher.AddHandler(
		handlers.Message{
			Callback:      injectContext,
			Filters:       nil,
			UpdateFilters: nil,
			Outgoing:      true,
		},
	)

injectContext:

func injectContext(ctx *ext.Context, update *ext.Update) error {
	ctx.Context = log.
		With().
		Int("update_effective_message_id", update.EffectiveMessage.ID).
		Logger().
		WithContext(ctx)
        _, _ = ctx.Reply(update, update.EffectiveMessage.Message.Message, nil) // for debug purposes 
	return nil
}

If you are not familiar with zerolog, this code just creates new logger with injected field and uses context.WithValue to store it in ctx.
But this function always panics with large traceback:

runtime: goroutine stack exceeds 1000000000-byte limit
runtime: sp=0xc020740398 stack=[0xc020740000, 0xc040740000]
fatal error: stack overflow

runtime stack:
runtime.throw({0x1010f0601?, 0x101cdf180?})
        /usr/local/go/src/runtime/panic.go:1047 +0x5d fp=0x31e4f4d88 sp=0x31e4f4d58 pc=0x10003853d
runtime.newstack()
        /usr/local/go/src/runtime/stack.go:1105 +0x5bd fp=0x31e4f4f38 sp=0x31e4f4d88 pc=0x1000526bd
runtime.morestack()
        /usr/local/go/src/runtime/asm_amd64.s:574 +0x8b fp=0x31e4f4f40 sp=0x31e4f4f38 pc=0x100069eab

goroutine 41 [running]:
github.com/anonyindian/gotgproto/ext.(*Context).Done(0xc000694400?)
        <autogenerated>:1 +0x3f fp=0xc0207403a8 sp=0xc0207403a0 pc=0x100b3895f
context.(*valueCtx).Done(0x0?)
        <autogenerated>:1 +0x2a fp=0xc0207403c0 sp=0xc0207403a8 pc=0x1000b590a
github.com/anonyindian/gotgproto/ext.(*Context).Done(0x0?)
        <autogenerated>:1 +0x2b fp=0xc0207403d8 sp=0xc0207403c0 pc=0x100b3894b
context.(*valueCtx).Done(0x0?)
        <autogenerated>:1 +0x2a fp=0xc0207403f0 sp=0xc0207403d8 pc=0x1000b590a
github.com/anonyindian/gotgproto/ext.(*Context).Done(0x0?)
        <autogenerated>:1 +0x2b fp=0xc020740408 sp=0xc0207403f0 pc=0x100b3894b
context.(*valueCtx).Done(0x0?)
...

Am I doing something wrong? Are there any ways to use WithValue with ext.Context?

Used dependencies:

	github.com/anonyindian/gotgproto v1.0.0-beta11
	github.com/gotd/td v0.82.0
	github.com/rs/zerolog v1.30.0

(I've deleted unrelated)

Were you able to fix the issue?

Were you able to fix the issue?

Yep, idk how, but with GoTGProto v1.0.0-beta14 version it works.
The only thing i have changed was removing _, _ = ctx.Reply(update, update.EffectiveMessage.Message.Message, nil) // for debug purposes, maybe this line of code was forcing gotgproto to run in infinite recursion?