AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Socket error (server crash) (https://forums.alliedmods.net/showthread.php?t=265537)

Guxi 06-29-2015 20:17

Socket error (server crash)
 
(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
http://i.imgur.com/Bg9Skcm.png


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.

OvidiuS 06-30-2015 08:27

Re: Socket error (server crash)
 
Quote:

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


klippy 06-30-2015 10:43

Re: Socket error (server crash)
 
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.

HamletEagle 06-30-2015 11:07

Re: Socket error (server crash)
 
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.

Destro- 06-30-2015 15:25

Re: Socket error (server crash)
 
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 (Post 2313901)
@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

Guxi 07-01-2015 05:23

Re: Socket error (server crash)
 
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.

klippy 07-01-2015 08:00

Re: Socket error (server crash)
 
Quote:

Originally Posted by Guxi (Post 2314188)
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.


All times are GMT -4. The time now is 22:54.

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