Skip to content

Middleware logic in turncontext emit is broken #2197

Open
@heyitsaamir

Description

@heyitsaamir

Github issues should be used for bugs and feature requests. Use Stack Overflow for general "how-to" questions.

Version

4.16.2

Describe the bug

The turncontext middlware chain is as follows:

handlers = copy(plugins)
async def emit_next(i: int):
context = self
try:
if i < len(handlers):
async def next_handler():
await emit_next(i + 1)
await handlers[i](context, arg, next_handler)
except Exception as error:
raise error
await emit_next(0)
# logic does not use parentheses because it's a coroutine
return await logic

Notice how the "logic" runs after all the middlware has run? This prevents any middlware from awaiting the result of the "next" middleware. This means that if you wanted to wait for the "response" from the last middleware (which runs the actual logic to send the activity), you can't.

This is in contrast with how the middleware for js is built:
https://github.com/microsoft/botbuilder-js/blob/3b8fcab21a0a5434706da8eace17117722ffd78b/libraries/botbuilder-core/src/turnContext.ts#L864.

Here, if you wanted to do:

async function MyMiddleware(context, args, next) {
   console.log(args)
   const res = await next()
   console.log(res)
}

you can. But in python, the same thing would not be possible because the handlers don't wait for the last handler (which is the actual logic) to be run in the same chain as all the handlers.

Metadata

Metadata

Assignees

Labels

bugIndicates an unexpected problem or an unintended behavior.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions