Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6144165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:36:47+00:00 2026-05-23T18:36:47+00:00

Background: I’m using a Lua thread (coroutine) to process user input from stdin (to

  • 0

Background: I’m using a Lua thread (coroutine) to process user input from stdin (to allow the program to pause while waiting on data from another FD). Because it’s user input, errors are possible if not probable, e.g. calling a non-existent function.

Question: Can the Lua thread be recovered so that I can continue to process more data from stdin, or do I have to nuke the thread and create a new one after every error?

Here’s some rough sample/pseudo code of what I’m doing now:

while (1) {
  select((max, &read_fds, &write_fds, NULL, NULL);
  for each file descriptor {
    if (read fd is set) {
      read data into a buffer
      if (current fd is stdin)
        process_stdin()
      else if (current fd is from server connection)
        process_remote()
    }
    if (write fd is set) {
      write data on non-blocking fd
    }
  }
}

process_stdin() {
  status=luaL_loadbuffer(L, stdin_buf, len, "stdin");
  if (status == LUA_ERRSYNTAX) {
    /* handle EOF which means more user input needed
     * or print error message for user, this works fine */
  }
  else if (status == 0) {
    status=lua_resume(L, 0);
    if (status != 0 && status != LUA_YIELD) {
      /* Do I nuke the thread or is there another way to recover at this point??? */
    }
  }
}

Typically, I would use pcall to catch the error and recover, but pcall doesn’t support the yield in 5.1 (though 5.2 may be a good solution here). With the lua_resume call, I’m running into the following in my session:

> no_such_func()
Error: attempt to call global 'no_such_func' (a nil value)
> print("hello world")
Error: cannot resume non-suspended coroutine

After the first command, the thread status is set to 2 (LUA_ERRRUN).

Edit: the error message I’m getting doesn’t appear to be because of the unwound stack. I’m seeing this message from ldo.c which indicates the issue is because the thread state is getting set to 2.

  if (L->status != LUA_YIELD && (L->status != 0 || L->ci != L->base_ci))
      return resume_error(L, "cannot resume non-suspended coroutine");

So I either need a way to reset the state or avoid having the state change in the first place. My guess is that I may be stuck with popping the thread off my main stack and recreating a new one, or upgrading to 5.2 so I can yield from a pcall.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:36:48+00:00Added an answer on May 23, 2026 at 6:36 pm

    For Lua 5.1, you can use coxpcall.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Background: I'm in the process of creating a web service using ASP.NET 2.0. This
BACKGROUND: Co-worker Adam has been using Google refine to process database downloads with much
Background: At my company we are developing a bunch applications that are using the
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background The Von-Neumann architecture describes the stored-program computer where instructions and data are stored
Background: I've wrote a small library that is able to create asp.net controls from
BACKGROUND I am automating an PowerPoint 2007 via C# I am writing unittests using
Background: I am using Eclipse to develop an Android app. I have an xml
Background In the spirit of continuous integration, I'm trying to automate our deployment process
Background: One of the problems with using a local static variable in a function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.