Skip to content
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

What is the correct way to run nested dostring? Avoid "Lua state is running" errors #97

Open
CodeSmile-0000011110110111 opened this issue Jan 24, 2025 · 1 comment

Comments

@CodeSmile-0000011110110111
Copy link

CodeSmile-0000011110110111 commented Jan 24, 2025

In lack of a support channel (is there one?) I'm creating an issue for this.

Use case:
Run a script, that calls a C# method, which runs DoStringAsync in the same state. This leads to "lua state is running" errors.

Current workaround:
I copied the original dostring method from the library which parses the string and runs the compiler.

Is this the intended way to run nested dostrings, or are there alternatives?

@CodeSmile-0000011110110111
Copy link
Author

Ultimately it turned out to be a classic multithreading issue with a non-shareable resource, thus you need to lock it.

So in my Lua wrapper class I safeguard multiple awaited dostrings with a semaphore now:

private readonly SemaphoreSlim m_DoStringSemaphore = new(1, 1);

public async Task<LuaValue[]> DoStringAsync(String script, String chunkName = null)
{
	// semaphore to avoid "the lua state is currently running" InvalidOperationException
	await m_DoStringSemaphore.WaitAsync();

	LuaValue[] result;
	try
	{
		result = await m_LuaState.DoStringAsync(script, chunkName);
	}
	finally
	{
		m_DoStringSemaphore.Release();
	}
	return result;
}

Works like a charm.

Still curious if there's a support channel with other users, Discord perhaps?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant