pgpool-II3.1 的内存泄漏(五)
磨砺技术珠矶,践行数据之道,追求卓越价值
回到上一级页面: PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页
[作者 高健@博客园 luckyjackgao@gmail.com]
情形B read_startup_packet 调用 calloc
==27927== 594 (528 direct, 66 indirect) bytes in 11 blocks are definitely lost in loss record 85 of 100
==27927== at 0x4A05140: calloc (vg_replace_malloc.c:418)
==27927== by 0x40895B: read_startup_packet (child.c:803)
==27927== by 0x409663: do_child (child.c:210)
==27927== by 0x403F04: fork_a_child (main.c:1073)
==27927== by 0x406C00: main (main.c:550)
从上述log可知, 调用关系如下:
main-> fork_a_child -> do_child -> read_startup_packet -> calloc
以下是各个函数的主要相关逻辑:
main函数:
/*
* pgpool main program
*/
int main(int argc, char **argv)
{
……
/*
* We need to block signal here. Otherwise child might send some
* signals, for example SIGUSR1(fail over). Children will inherit
* signal blocking but they do unblock signals at the very beginning
* of process. So this is harmless.
*/
POOL_SETMASK(&BlockSig);
/* fork the children */
for (i=0;i<pool_config->num_init_children;i++)
{
process_info[i].pid = fork_a_child(unix_fd, inet_fd, i);
process_info[i].start_time = time(NULL);
}
/* set up signal handlers */
pool_signal(SIGTERM, exit_handler);
pool_signal(SIGINT, exit_handler);
pool_signal(SIGQUIT, exit_handler);
pool_signal(SIGCHLD, reap_handler);
pool_signal(SIGUSR1, failover_handler);
pool_signal(SIGUSR2, wakeup_handler);
pool_signal(SIGHUP, reload_config_handler);
/* create pipe for delivering event */
if (pipe(pipe_fds) < 0)
{
pool_error("failed to create pipe");
myexit(1);
}
……
/*
* This is the main loop
*/
for (;;)
{
……
}
pool_shmem_exit(0);
}
for_a_child函数:
/*
* fork a child
*/
pid_t fork_a_child(int unix_fd, int inet_fd, int id)
{
pid_t pid;
pid = fork();
if (pid == 0)
{
/* Before we unconditionally closed pipe_fds[0] and pipe_fds[1]
* here, which is apparently wrong since in the start up of
* pgpool, pipe(2) is not called yet and it mistakenly closes
* fd 0. Now we check the fd > 0 before close(), expecting
* pipe returns fds greater than 0. Note that we cannot
* unconditionally remove close(2) calls since fork_a_child()
* may be called *after* pgpool starting up.
*/
if (pipe_fds[0] > 0)
{
close(pipe_fds[0]);
close(pipe_fds[1]);
}
myargv = save_ps_display_args(myargc, myargv);
/* call child main */
POOL_SETMASK(&UnBlockSig);
reload_config_request = 0;
my_proc_id = id;
run_as_pcp_child = false;
do_child(unix_fd, inet_fd);
}
else if (pid == -1)
{
pool_error("fork() failed. reason: %s", strerror(errno));
myexit(1);
}
return pid;
}
do_child函数:
/* * child main loop */ void do_child(int unix_fd, int inet_fd) { …… for (;;){ StartupPacket *sp; …… /* perform accept() */ frontend = do_accept(unix_fd, inet_fd, &timeout); …… /* read the startup packet */ retry_startup: sp = read_startup_packet(frontend); if (sp == NULL){ /* failed to read the startup packet. return to the accept() loop */ pool_close(frontend); connection_count_down(); continue; } …… /* * Ok, negotiaton with frontend has been done. Let's go to the * next step. Connect to backend if there's no existing * connection which can be reused by this frontend. * Authentication is also done in this step. */ …… /* query process loop */ for (;;){ …… } /* Destroy session context */ pool_session_context_destroy(); /* Mark this connection pool is not conncted from frontend */ pool_coninfo_unset_frontend_connected
(pool_get_process_context()->proc_id, pool_pool_index()); accepted = 0; connection_count_down(); timeout.tv_sec = pool_config->child_life_time; timeout.tv_usec = 0; /* increment queries counter if necessary */ if ( pool_config->child_max_connections > 0 ) connections_count++; /* check if maximum connections count for this child reached */ if ( ( pool_config->child_max_connections > 0 ) && ( connections_count >= pool_config->child_max_connections ) ){ pool_log("child exiting, %d connections reached",
pool_config->child_max_connections); send_frontend_exits(); child_exit(2); } } child_exit(0); }
read_startup_packet 函数:
/*
* Read startup packet
*
* Read the startup packet and parse the contents.
*/
static StartupPacket *read_startup_packet(POOL_CONNECTION *cp)
{
StartupPacket *sp;
……
sp = (StartupPacket *)calloc(sizeof(*sp), 1);
if (!sp)
{
pool_error("read_startup_packet: out of memory");
return NULL;
}
……
sp->startup_packet = calloc(len, 1);
if (!sp->startup_packet)
{
pool_error("read_startup_packet: out of memory");
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
……
switch(sp->major)
{
case PROTO_MAJOR_V2: /* V2 */
sp2 = (StartupPacket_v2 *)(sp->startup_packet);
sp->database = calloc(SM_DATABASE+1, 1);
if (!sp->database)
{
pool_error("read_startup_packet: out of memory");
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
……
break;
case 1234: /* cancel or SSL request */
/* set dummy database, user info */
sp->database = calloc(1, 1);
if (!sp->database)
{
pool_error("read_startup_packet: out of memory");
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
sp->user = calloc(1, 1);
if (!sp->user)
{
pool_error("read_startup_packet: out of memory");
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
break;
default:
pool_error("read_startup_packet: invalid major no: %d", sp->major);
pool_free_startup_packet(sp);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return NULL;
}
pool_debug("Protocol Major: %d Minor: %d database: %s user: %s",
sp->major, sp->minor, sp->database, sp->user);
alarm(0);
pool_signal(SIGALRM, SIG_IGN);
return sp;
}
由以上各个函数的逻辑,可以看到
在 read_startup_packet 函数中,调用 calloc 来开了内存。
但是在do_child 函数的主循环中,会反复使用 read_startup_packet 函数,并且也未能释放内存。
由于 do_child 函数的反复循环,就会反复开内存而不释放。
这里确确实实地 发生了 内存泄露。
其实,它的do_child函数的循环的末尾,应该释放 StartupPacket *sp 才好,不知为何不去释放。
[作者 高健@博客园 luckyjackgao@gmail.com]
回到上一级页面: PostgreSQL集群方案相关索引页 回到顶级页面:PostgreSQL索引页
磨砺技术珠矶,践行数据之道,追求卓越价值