Raised This Month: $32 Target: $400
 8% 

Socket error (server crash)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Guxi
Junior Member
Join Date: Aug 2014
Old 06-29-2015 , 20:17   Socket error (server crash)
Reply With Quote #1

(This thread was by accident posted in SourcePawn section. It's deleted and reposted here.)

I made this pretty simple plugin which sends some data via TCP protocol using basic sockets.
Most of the time, everything works fine -- plugin successfully sends data to remote server, remote server receives it.

However, sometimes (rarerly) this error happens and causes server to crash.

Plugin error log



I do not see origin of this error, neither i see a way to find out what causes this to happen.

Does this happen usually when using sockets?
If so, is there a method to prevent this from happening or possibility of catching this error before it occurs?


Source:
PHP Code:
/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <sockets>
#include <amxmisc>
#include < fun >
#include < cstrike >
#include < hamsandwich >
#include <ColorChat>
#include <fakemeta>

#define PLUGIN "ESB test"
#define VERSION "0.1"
#define AUTHOR "Ante Gulin"
new sckesb_server

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_clcmd("say !esb","about")
    
register_event("DeathMsg""Event_DeathMsg""a"// Register death event
}

public 
about(id)
{
    
client_print(id,print_chat,"This is a ESB test plugin.");
}
public 
Event_DeathMsg()
{
    
    new 
iKiller read_data(1
    new 
iVictim read_data(2)
    new 
name_killer[35
    new 
name_victim[35
    new 
killer_tmvictim_tm;            
    
get_user_name(iKiller,name_killer,34);
    
get_user_name(iVictim,name_victim,34);
    
    
killer_tm get_user_team(iKiller);
    
victim_tm get_user_team(iVictim);
    
    new 
packetdata[512]
    
format(packetdata512"31|1|%s|%s|%d|%d"name_killername_victim,killer_tmvictim_tm)
    
    if(
is_user_alive(iKiller))
    {
        
connect_esb_server
    write_esb_server
(packetdata);
    
      
    }
}  

/* Socket esb_server */

public connect_esb_server() {
    new 
error 0                       /*IP hidden*/
    
sckesb_server socket_open("xx.xx.xx.xx"61000SOCKET_TCPerror)
    if (
sckesb_server 0) {
        
/* Connect successful */
        
read_esb_server()
    }
    else {
        switch (
error) {
            case 
1: { /* Error creating socket */ }
            case 
2: { /* Could not resolve hostname */ }
            case 
3: { /* Could not connect to given host:port */ }
        }
    }
}

public 
read_esb_server() {
    if (
socket_change(sckesb_server100)) {
        new 
buf[512], lines[30][100], count 0
        socket_recv
(sckesb_serverbuf511)
        
count ExplodeString(lines2999buf13)
        for(new 
i=0;i<count;i++) {
            
/* Process items here */
        
}
    }
    
    if (
sckesb_server != 0)
        
set_task(0.5"read_esb_server")
    else {
        
set_cvar_num("amx_esb_server_socket"0)
        
disconnect_esb_server()
    }
}

public 
write_esb_server(text[512]) {
    
socket_send(sckesb_servertext511)
    
disconnect_esb_server()
}

public 
disconnect_esb_server() {
    
/* Disconnected */
}

stock ExplodeStringp_szOutput[][], p_nMaxp_nSizep_szInput[], p_szDelimiter ) { // Function by xeroblood
    
new nIdx 0strlen(p_szInput)
    new 
nLen = (copycp_szOutput[nIdx], p_nSizep_szInputp_szDelimiter ))
    while( (
nLen l) && (++nIdx p_nMax) )
        
nLen += (copycp_szOutput[nIdx], p_nSizep_szInput[nLen], p_szDelimiter ))
    return 
nIdx

Thank you in advance for your time, i appreciate it.
Guxi is offline
OvidiuS
Chillaxin'
Join Date: Dec 2009
Location: Serbia
Old 06-30-2015 , 08:27   Re: Socket error (server crash)
Reply With Quote #2

Quote:
Originally Posted by SchlumPF* View Post
a stackerror occures if the heapsize is full, this usually occures on to huge/many variables or any leak in your plugin.

Last edited by OvidiuS; 06-30-2015 at 08:28.
OvidiuS is offline
Send a message via Skype™ to OvidiuS
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 06-30-2015 , 10:43   Re: Socket error (server crash)
Reply With Quote #3

Stack/heap size is 4096 cells (16384 bytes) by default, and you've exceeded that amount with your plugin by allocating more memory on stack than you are allowed to (probably with all those huge arrays you have in your plugin). Try adding
PHP Code:
// 4096 is by default, this adds 2048 cells (8192 bytes) to stack/heap block size
#pragma dynamic 6144 
on top of your code. If the error still occurs, try adding more (8192 for instance), but it shouldn't, I believe.

Last edited by klippy; 06-30-2015 at 10:45.
klippy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-30-2015 , 11:07   Re: Socket error (server crash)
Reply With Quote #4

In one of my plugins I use bigger arrays(like 2000 cells), of course I got a stack error, but the solution was to make the vars static so they will be initialized only one time.

@Klippy, the 4096 limit refers to only one variable or it is the sum of all of them ? I had the impression that it is only for one.
__________________

Last edited by HamletEagle; 06-30-2015 at 11:08.
HamletEagle is offline
Destro-
Veteran Member
Join Date: Jun 2010
Location: $me->location();
Old 06-30-2015 , 15:25   Re: Socket error (server crash)
Reply With Quote #5

to have in mind
socket_change will block the server if you use timeout, same for socket_open if not is localhost

Quote:
Originally Posted by HamletEagle View Post
@Klippy, the 4096 limit refers to only one variable or it is the sum of all of them ? I had the impression that it is only for one.
sum of all of them per recursive execution

Spoiler
__________________

Last edited by Destro-; 06-30-2015 at 15:32.
Destro- is offline
Guxi
Junior Member
Join Date: Aug 2014
Old 07-01-2015 , 05:23   Re: Socket error (server crash)
Reply With Quote #6

I increased heapsize as KliPPy suggested, and declared those variable in global; static scope as HamletEagle suggested. I'll let the plugin roll on 5-10 cs servers for few days to see if it happens again, thanks.
Guxi is offline
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 07-01-2015 , 08:00   Re: Socket error (server crash)
Reply With Quote #7

Quote:
Originally Posted by Guxi View Post
I increased heapsize as KliPPy suggested, and declared those variable in global; static scope as HamletEagle suggested. I'll let the plugin roll on 5-10 cs servers for few days to see if it happens again, thanks.
You don't have to both increase the stack/heap size and make all those arrays static local/global. Making them static in local scope would probably be the best choice.
klippy 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 07:38.


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