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 6330569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:53:44+00:00 2026-05-24T17:53:44+00:00

How do you end a long running Lua script? I have two threads, one

  • 0

How do you end a long running Lua script?

I have two threads, one runs the main program and the other controls a user supplied Lua script. I need to kill the thread that’s running Lua, but first I need the script to exit.

Is there a way to force a script to exit?

I have read that the suggested approach is to return a Lua exception. However, it’s not garanteed that the user’s script will ever call an api function ( it could be in a tight busy loop). Further, the user could prevent errors from causing his script to exit by using 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-24T17:53:44+00:00Added an answer on May 24, 2026 at 5:53 pm

    You could use setjmp and longjump, just like the Lua library does internally. That will get you out of pcalls and stuff just fine without need to continuously error, preventing the script from attempting to handle your bogus errors and still getting you out of execution. (I have no idea how well this plays with threads though.)

    #include <stdio.h>
    #include <setjmp.h>
    #include "lua.h"
    #include "lualib.h"
    #include "lauxlib.h"
    
    jmp_buf place;
    
    void hook(lua_State* L, lua_Debug *ar)
    {
        static int countdown = 10;
        if (countdown > 0)
        {
            --countdown;
            printf("countdown: %d!\n", countdown);
        }
        else
        {
            longjmp(place, 1);
        }
    }
    
    int main(int argc, const char *argv[])
    {
        lua_State* L = luaL_newstate();
        luaL_openlibs(L);
        lua_sethook(L, hook, LUA_MASKCOUNT, 100);
    
        if (setjmp(place) == 0)
            luaL_dostring(L, "function test() pcall(test) print 'recursing' end pcall(test)");
    
        lua_close(L);
        printf("Done!");
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built a long running script to which I have added a progress
I have a long-running script that may or may not have an uncomitted transaction
I have a long running program, my_prog that has some output that I want
I have a pretty long running job, which runs for several minutes and then
I have a deal where file1.php curl runs file2.php. file2.php is a long running
I have a regex that is going to end up being a bit long
I've got a long running python script that I want to be able to
I have a long running query that returns a large data set. This query
I'm writing a fairly long running script, which prints progress messages as it goes.
I have an audit table which stores audit rows for a long running process

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.