Raised This Month: $12 Target: $400
 3% 

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


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 04-23-2020 , 23:52   Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #1

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

Last edited by Th3822; 04-26-2020 at 20:48.
Th3822 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-26-2020 , 13:43   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #2

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.
__________________
fysiks is offline
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 04-26-2020 , 14:47   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #3

Quote:
Originally Posted by fysiks View Post
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?
Th3822 is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-26-2020 , 14:51   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #4

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).
__________________

Last edited by fysiks; 04-26-2020 at 14:52.
fysiks is offline
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 04-26-2020 , 15:09   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #5

Quote:
Originally Posted by fysiks View Post
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
Th3822 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-26-2020 , 16:34   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #6

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.
__________________
klippy is offline
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 04-26-2020 , 17:00   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #7

Quote:
Originally Posted by KliPPy View Post
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...
Th3822 is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-26-2020 , 18:25   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #8

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.
__________________

Last edited by klippy; 04-26-2020 at 18:26.
klippy is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 04-26-2020 , 19:29   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #9

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.
__________________

Last edited by klippy; 04-26-2020 at 19:33.
klippy is offline
Th3822
Member
Join Date: Jan 2013
Location: Venezuela
Old 04-26-2020 , 20:44   Re: Weird error/crash after using server_cmd("stats") and server_exec() on a clcmd
Reply With Quote #10

Quote:
Originally Posted by KliPPy View Post
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
Th3822 is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:40.


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