user6269864
user6269864

Reputation:

"Stack is empty" in Microsoft Bot Framework

I am writing a bot using the Microsoft Bot Framework. It consists of a MessagesController that calls a LuisDialog using await Conversation.SendAsync....

Some scenarios trigger an exception with this message:

Stack is empty

I found the place in the source code of the Bot Framework where it is triggered:

  134         public static T Peek<T>(this IList<T> stack)
  135         {
  136             if (stack.Count == 0)
  137             {
  138                 throw new InvalidOperationException("Stack is empty");
  139             }
  140 
  141             return stack[stack.Count - 1];
  142         }

However, I cannot figure out how exactly it is triggered. When debugging line by line, the code seems to work fine (there's a lot of code, and it goes through all of it).

When just letting it run without breakpoints, it crashes with this exception.

My await Conversation.SendAsync is wrapped into a try/catch and this is how I catch the exception.

I couldn't figure out more about which part of code exactly causes the exception.

Do you have any idea what could cause the "Stack is empty" message?

Later update:

Some time later, I started getting the Stack is Empty exceptions in another piece of code, and I made sure everything is asynced and awaited:

public async Task StartAsync(IDialogContext context)
{
    context.Wait(UserProfileStep1);
}

public async Task UserProfileStep1(IDialogContext context, IAwaitable<object> original)
{
    string originalMessage = (string)await original; // <== the exception occurs here

Turns out that in that particular case, it was because I used context.Forward and tried to pass a string object into it, while it only supports IMessageActivity objects.

Upvotes: 1

Views: 1354

Answers (2)

Manish
Manish

Reputation: 113

context.Done<object>(null);

Use this in the last of StartAsync.

Upvotes: 0

The Memebot
The Memebot

Reputation: 3549

To me it looks like you are missing an await operator somewhere. I ran into this exception before (even with the same issue) when calling an external API an wasn't awaiting for it to complete, so when i was debugging i was giving time, but in real-time the bot was faster than the API. I would need to look at your code for giving more info

Upvotes: 2

Related Questions