AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd (https://forums.alliedmods.net/showthread.php?t=323623)

Th3822 04-23-2020 23:52

Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
I found this weird bug while testing a plugin (that i will be release soon(tm))

First, here is an example plugin for testing the issue:
PHP Code:

#include <amxmodx>

new const g_szMessage[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.^nMorbi condimentum ex eu arcu suscipit, ac sodales quam pulvinar.";

public 
plugin_init()
{
    
register_plugin("server_cmd(^"stats^");se""rver_exec("") bug/crash");
    
register_concmd("test""cmdTest");
    
register_concmd("test2""cmdTest2");
}

public 
cmdTest(id)
{
    
server_cmd("stats");
    
console_print(idg_szMessage);
    return 
PLUGIN_HANDLED;
}

public 
cmdTest2(id)
{
    
server_cmd("stats");
    
server_exec();
    
console_print(idg_szMessage);
    return 
PLUGIN_HANDLED;


When using "test", it works, stats is shown on server console and the message is printed on client's console.
But, when running "test2", at client side, the message is shown, but at server, it shows an extra error msg:
Code:

CPU  In    Out  Uptime  Users  FPS    Players
 0.00  2.40  1.62      0    1  57.29      1
SV_ReadClientMessage: badread

And it's not only that, after a couple of uses/minutes it randomly crashes the server.
Here is a message shown at the crash:
Code:

SV_ReadClientMessage: too many cmds 171 sent for /00000000:000000000000:0
L 04/19/2020 - 12:12:03: FATAL ERROR (shutting down): SZ_GetSpace:  Tried to write to an uninitialized sizebuf_t: ???

* I've noticed that the issue doesn't happen when i run "test2" from server's console, nor the error msg or the crash, it just happens when it's called by a client

* Issue happens even without the console_print, but server crashes quickly with it (< 3-5 min), so i did leave it

* I've already tested with 1.8.2 and a 1.9.0 build on windows [ cstrike ] (both HLDS/reHLDS), also with the same 1.9.0 build on linux with reHLDS, the issue is present on both.

* I've tested some other commands, but it only seems to happen with "stats"

btw, i really need the server_exec there, so deleting it's not an option.

Edit: Answer

fysiks 04-26-2020 13:43

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
A very long time ago, this happened to me every time I used server_exec() on my Linux test server. However, in the last couple years, I tested it again and it won't crash it. I tested your code and both with and without the server_exec() produce the stats output and neither crashed it.

Th3822 04-26-2020 14:47

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
Quote:

Originally Posted by fysiks (Post 2696375)
A very long time ago, this happened to me every time I used server_exec() on my Linux test server. However, in the last couple years, I tested it again and it won't crash it. I tested your code and both with and without the server_exec() produce the stats output and neither crashed it.

Did you get the badread error?
Also, did you test the commands as a player on the server?

fysiks 04-26-2020 14:51

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
I didn't get any errors. I tested from the server's console (technically this shouldn't matter since server_cmd() sends the command to the server regardless of how the function gets called).

Th3822 04-26-2020 15:09

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
Quote:

Originally Posted by fysiks (Post 2696410)
I didn't get any errors. I tested from the server's console (technically this shouldn't matter since server_cmd() sends the command to the server regardless of how the function gets called).

i already said that only happens when it used by a player, please test as a player

klippy 04-26-2020 16:34

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
I may be talking out of my arse, but it may be related to the fact that you are telling the engine to execute the command buffer immediately while it's processing a client command already.
Do you have to execute the command immediately? If not, try postponing it a single frame by using RequestFrame.

Th3822 04-26-2020 17:00

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
Quote:

Originally Posted by KliPPy (Post 2696425)
I may be talking out of my arse, but it may be related to the fact that you are telling the engine to execute the command buffer immediately while it's processing a client command already.
Do you have to execute the command immediately? If not, try postponing it a single frame by using RequestFrame.

unfortunately, yes.

I've a plugin that it's like server_cmd(), but it returns the command output as a string by using condebug, made as an alternative of hooking Con_Printf with orpheu (that doesn't crash when it's second arg is not an string)

the plugin it's done and i was about to post it (it was meant to be an stock, but i had to make it a plugin with api after noticing some issues)

this issue was one of the delays for it, i even tested cvarlist and "maps *" and no issue with them...

klippy 04-26-2020 18:25

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
Can you run the server through GDB (or other debugger) and get the stack trace once it crashes? It could help us fix the issue, or at least find the cause and avoid it somehow.

klippy 04-26-2020 19:29

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
I think I found the issue.
SV_ExecuteClientMessage mutates host_client and uses it in the loop while parsing/executing client messages. Once it gets to the client command message containing "test2", it will (a couple of stack frames deeper) result in your cmdTest2 command handler being called. The thing is that GetStatsString also mutates host_client, so something surely goes awry there.

Possible solutions?
1. Don't execute the stats command while in a command handler, or more precisely - don't force the engine to immediately execute the command buffer (server_exec).
2. Save and restore host_client using some Orpheu magic.

Th3822 04-26-2020 20:44

Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
 
Quote:

Originally Posted by KliPPy (Post 2696445)
I think I found the issue.
SV_ExecuteClientMessage mutates host_client and uses it in the loop while parsing/executing client messages. Once it gets to the client command message containing "test2", it will (a couple of stack frames deeper) result in your cmdTest2 command handler being called. The thing is that GetStatsString also mutates host_client, so something surely goes awry there.

Possible solutions?
1. Don't execute the stats command while in a command handler, or more precisely - don't force the engine to immediately execute the command buffer (server_exec).
2. Save and restore host_client using some Orpheu magic.

i should go with the 1
so i will need to write another version of the func using callback and wrapping the server_cmd with a pre and post command


All times are GMT -4. The time now is 05:45.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.