Skip to content

Middleware logic in turncontext emit is broken #2197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
heyitsaamir opened this issue Dec 18, 2024 · 0 comments · May be fixed by #2213
Open

Middleware logic in turncontext emit is broken #2197

heyitsaamir opened this issue Dec 18, 2024 · 0 comments · May be fixed by #2213
Assignees
Labels
bug Indicates an unexpected problem or an unintended behavior.

Comments

@heyitsaamir
Copy link

heyitsaamir commented Dec 18, 2024

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.

@heyitsaamir heyitsaamir added bug Indicates an unexpected problem or an unintended behavior. needs-triage The issue has just been created and it has not been reviewed by the team. labels Dec 18, 2024
corinagum pushed a commit to microsoft/teams-agent-accelerator-libs-py that referenced this issue Dec 18, 2024
- Adds a middlware for memory_module. Now, whenever a message from a
user comes in, or message is sent from the agent out back to the user,
it's automatically captured by the supplied memory_module.

Discovered a number of bugs in botframework-python. Added them:
microsoft/botbuilder-python#2197
microsoft/botbuilder-python#2198
@tracyboehrer tracyboehrer removed the needs-triage The issue has just been created and it has not been reviewed by the team. label Jan 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or an unintended behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants