Skip to content

Commit

Permalink
Init ICProxy's SHM variable in CreateSharedMemoryAndSemaphores() (#16…
Browse files Browse the repository at this point in the history
…586)

My previous commit 8915cd0 caused coredump in some pipeline jobs.
Example stack:
```
Core was generated by `postgres:  7000, ic proxy process
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000000000b46ec3 in pg_atomic_read_u32_impl (ptr=0x7f05a8c51104) at ../../../../src/include/port/atomics/generic.h:48
(gdb) bt
#0  0x0000000000b46ec3 in pg_atomic_read_u32_impl (ptr=0x7f05a8c51104) at ../../../../src/include/port/atomics/generic.h:48
#1  pg_atomic_read_u32 (ptr=0x7f05a8c51104) at ../../../../src/include/port/atomics.h:247
#2  LWLockAttemptLock (mode=LW_EXCLUSIVE, lock=0x7f05a8c51100) at lwlock.c:751
#3  LWLockAcquire (lock=0x7f05a8c51100, mode=mode@entry=LW_EXCLUSIVE) at lwlock.c:1188
#4  0x0000000000b32fff in ShmemInitStruct (name=name@entry=0x130e160 "", size=size@entry=4, foundPtr=foundPtr@entry=0x7ffcf94513bf) at shmem.c:412
#5  0x0000000000d6d18e in ic_proxy_server_main () at ic_proxy_main.c:545
#6  0x0000000000d6c219 in ICProxyMain (main_arg=<optimized out>) at ic_proxy_bgworker.c:36
#7  0x0000000000aa9caa in StartBackgroundWorker () at bgworker.c:955
#8  0x0000000000ab9407 in do_start_bgworker (rw=<optimized out>) at postmaster.c:6450
#9  maybe_start_bgworkers () at postmaster.c:6706
#10 0x0000000000abbc59 in ServerLoop () at postmaster.c:2095
#11 0x0000000000abd777 in PostmasterMain (argc=argc@entry=5, argv=argv@entry=0x36e3650) at postmaster.c:1633
#12 0x00000000006e4764 in main (argc=5, argv=0x36e3650) at main.c:240
(gdb) p *ptr
Cannot access memory at address 0x7f05a8c51104
```

The root cause is I forgot to init SHM structure at CreateSharedMemoryAndSemaphores().
Fix it in this commit.
  • Loading branch information
interma authored Oct 16, 2023
1 parent efedbc2 commit 13a4c03
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 36 deletions.
19 changes: 0 additions & 19 deletions src/backend/cdb/motion/ic_proxy_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -518,25 +518,6 @@ ic_proxy_backend_init_context(ChunkTransportState *state)
uv_unref((uv_handle_t *)&context->connectTimer);
}

/*
* Check if current Segment's IC_PROXY listener failed
*/
bool
ic_proxy_backend_check_listener_failed(void)
{
bool found;
ic_proxy_peer_listener_failed = ShmemInitStruct("IC_PROXY Listener Failure Flag",
sizeof(*ic_proxy_peer_listener_failed),
&found);

Assert(ic_proxy_peer_listener_failed != NULL);
/* init it to 0 when the backend accesses it firstly */
if (!found)
pg_atomic_init_u32(ic_proxy_peer_listener_failed, 0);

return pg_atomic_read_u32(ic_proxy_peer_listener_failed) > 0;
}

/*
* Close the icproxy backend context
*/
Expand Down
2 changes: 0 additions & 2 deletions src/backend/cdb/motion/ic_proxy_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#define IC_PROXY_BACKEND_H

#include "postgres.h"
#include "port/atomics.h"

#include "cdb/cdbinterconnect.h"

Expand Down Expand Up @@ -48,6 +47,5 @@ extern void ic_proxy_backend_connect(ICProxyBackendContext *context,
extern void ic_proxy_backend_init_context(ChunkTransportState *state);
extern void ic_proxy_backend_close_context(ChunkTransportState *state);
extern void ic_proxy_backend_run_loop(ICProxyBackendContext *context);
extern bool ic_proxy_backend_check_listener_failed(void);

#endif /* IC_PROXY_BACKEND_H */
26 changes: 26 additions & 0 deletions src/backend/cdb/motion/ic_proxy_bgworker.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "postgres.h"

#include "storage/ipc.h"
#include "storage/shmem.h"

#include "cdb/ic_proxy_bgworker.h"
#include "ic_proxy_server.h"
Expand All @@ -35,3 +36,28 @@ ICProxyMain(Datum main_arg)
/* main loop */
proc_exit(ic_proxy_server_main());
}

/*
* the size of ICProxy SHM structure
*/
Size
ICProxyShmemSize(void)
{
Size size = 0;
size = add_size(size, sizeof(*ic_proxy_peer_listener_failed));
return size;
}

/*
* initialize ICProxy's SHM structure: only one flag variable
*/
void
ICProxyShmemInit(void)
{
bool found;
ic_proxy_peer_listener_failed = ShmemInitStruct("IC_PROXY Listener Failure Flag",
sizeof(*ic_proxy_peer_listener_failed),
&found);
if (!found)
pg_atomic_init_u32(ic_proxy_peer_listener_failed, 0);
}
11 changes: 1 addition & 10 deletions src/backend/cdb/motion/ic_proxy_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,19 +537,10 @@ int
ic_proxy_server_main(void)
{
char path[MAXPGPATH];
bool found;
elogif(gp_log_interconnect >= GPVARS_VERBOSITY_TERSE,
LOG, "ic-proxy: server setting up");

/* get and init failure flag */
ic_proxy_peer_listener_failed = ShmemInitStruct("IC_PROXY Listener Failure Flag",
sizeof(*ic_proxy_peer_listener_failed),
&found);
if (!found)
pg_atomic_init_u32(ic_proxy_peer_listener_failed, 0);
else
pg_atomic_exchange_u32(ic_proxy_peer_listener_failed, 0);

pg_atomic_exchange_u32(ic_proxy_peer_listener_failed, 0);
ic_proxy_pkt_cache_init(IC_PROXY_MAX_PKT_SIZE);

uv_loop_init(&ic_proxy_server_loop);
Expand Down
3 changes: 2 additions & 1 deletion src/backend/cdb/motion/ic_tcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,8 @@ SetupTCPInterconnect(EState *estate)
interconnect_context->doSendStopMessage = doSendStopMessageTCP;

#ifdef ENABLE_IC_PROXY
if (ic_proxy_backend_check_listener_failed())
/* check if current Segment's ICProxy listener failed */
if (pg_atomic_read_u32(ic_proxy_peer_listener_failed) > 0)
elog(ERROR, "SetupInterconnect: We are in IC_PROXY mode, but IC-Proxy Listener failed, please check.");
ic_proxy_backend_init_context(interconnect_context);
#endif /* ENABLE_IC_PROXY */
Expand Down
4 changes: 0 additions & 4 deletions src/backend/postmaster/postmaster.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,7 @@ static BackgroundWorker PMAuxProcList[MaxPMAuxProc] =

#ifdef ENABLE_IC_PROXY
{"ic proxy process", "ic proxy process",
#ifdef FAULT_INJECTOR
BGWORKER_SHMEM_ACCESS,
#else
0,
#endif
BgWorkerStart_RecoveryFinished,
0, /* restart immediately if ic proxy process exits with non-zero code */
"postgres", "ICProxyMain", 0, {0}, 0,
Expand Down
9 changes: 9 additions & 0 deletions src/backend/storage/ipc/ipci.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
#include "utils/session_state.h"
#include "cdb/cdbendpoint.h"
#include "replication/gp_replication.h"
#include "cdb/ic_proxy_bgworker.h"

/* GUCs */
int shared_memory_type = DEFAULT_SHARED_MEMORY_TYPE;
Expand Down Expand Up @@ -201,6 +202,10 @@ CreateSharedMemoryAndSemaphores(int port)
size = add_size(size, FaultInjector_ShmemSize());
#endif

#ifdef ENABLE_IC_PROXY
size = add_size(size, ICProxyShmemSize());
#endif

/* This elog happens before we know the name of the log file we are supposed to use */
elog(DEBUG1, "Size not including the buffer pool %lu",
(unsigned long) size);
Expand Down Expand Up @@ -355,6 +360,10 @@ CreateSharedMemoryAndSemaphores(int port)
FaultInjector_ShmemInit();
#endif

#ifdef ENABLE_IC_PROXY
ICProxyShmemInit();
#endif

/*
* Set up other modules that need some shared memory space
*/
Expand Down
7 changes: 7 additions & 0 deletions src/include/cdb/ic_proxy_bgworker.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,14 @@
#ifndef IC_PROXY_BGWORKER_H
#define IC_PROXY_BGWORKER_H

#include "port/atomics.h"

/* flag (in SHM) for incidaing if peer listener bind/listen failed */
extern pg_atomic_uint32 *ic_proxy_peer_listener_failed;

extern bool ICProxyStartRule(Datum main_arg);
extern void ICProxyMain(Datum main_arg);
extern Size ICProxyShmemSize(void);
extern void ICProxyShmemInit(void);

#endif /* IC_PROXY_BGWORKER_H */

0 comments on commit 13a4c03

Please sign in to comment.