Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2e08861

Browse files
committed
Fix file descriptors and memory leaks for backends
1 parente9f69ab commit2e08861

File tree

16 files changed

+144
-87
lines changed

16 files changed

+144
-87
lines changed

‎src/backend/commands/tablespace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,8 +1240,8 @@ check_temp_tablespaces(char **newval, void **extra, GucSource source)
12401240
}
12411241

12421242
/* Now prepare an "extra" struct for assign_temp_tablespaces */
1243-
myextra=malloc(offsetof(temp_tablespaces_extra,tblSpcs)+
1244-
numSpcs*sizeof(Oid));
1243+
myextra=top_malloc(offsetof(temp_tablespaces_extra,tblSpcs)+
1244+
numSpcs*sizeof(Oid));
12451245
if (!myextra)
12461246
return false;
12471247
myextra->numSpcs=numSpcs;

‎src/backend/commands/variable.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,15 @@ check_datestyle(char **newval, void **extra, GucSource source)
140140
char*subval;
141141
void*subextra=NULL;
142142

143-
subval=strdup(GetConfigOptionResetString("datestyle"));
143+
subval=top_strdup(GetConfigOptionResetString("datestyle"));
144144
if (!subval)
145145
{
146146
ok= false;
147147
break;
148148
}
149149
if (!check_datestyle(&subval,&subextra,source))
150150
{
151-
free(subval);
151+
top_free(subval);
152152
ok= false;
153153
break;
154154
}
@@ -157,8 +157,8 @@ check_datestyle(char **newval, void **extra, GucSource source)
157157
newDateStyle=myextra[0];
158158
if (!have_order)
159159
newDateOrder=myextra[1];
160-
free(subval);
161-
free(subextra);
160+
top_free(subval);
161+
top_free(subextra);
162162
}
163163
else
164164
{
@@ -181,7 +181,7 @@ check_datestyle(char **newval, void **extra, GucSource source)
181181
/*
182182
* Prepare the canonical string to return. GUC wants it malloc'd.
183183
*/
184-
result= (char*)malloc(32);
184+
result= (char*)top_malloc(32);
185185
if (!result)
186186
return false;
187187

@@ -213,13 +213,13 @@ check_datestyle(char **newval, void **extra, GucSource source)
213213
break;
214214
}
215215

216-
free(*newval);
216+
top_free(*newval);
217217
*newval=result;
218218

219219
/*
220220
* Set up the "extra" struct actually used by assign_datestyle.
221221
*/
222-
myextra= (int*)malloc(2*sizeof(int));
222+
myextra= (int*)top_malloc(2*sizeof(int));
223223
if (!myextra)
224224
return false;
225225
myextra[0]=newDateStyle;
@@ -358,7 +358,7 @@ check_timezone(char **newval, void **extra, GucSource source)
358358
/*
359359
* Pass back data for assign_timezone to use
360360
*/
361-
*extra=malloc(sizeof(pg_tz*));
361+
*extra=top_malloc(sizeof(pg_tz*));
362362
if (!*extra)
363363
return false;
364364
*((pg_tz**)*extra)=new_tz;
@@ -431,7 +431,7 @@ check_log_timezone(char **newval, void **extra, GucSource source)
431431
/*
432432
* Pass back data for assign_log_timezone to use
433433
*/
434-
*extra=malloc(sizeof(pg_tz*));
434+
*extra=top_malloc(sizeof(pg_tz*));
435435
if (!*extra)
436436
return false;
437437
*((pg_tz**)*extra)=new_tz;
@@ -574,7 +574,7 @@ check_XactIsoLevel(char **newval, void **extra, GucSource source)
574574
}
575575
}
576576

577-
*extra=malloc(sizeof(int));
577+
*extra=top_malloc(sizeof(int));
578578
if (!*extra)
579579
return false;
580580
*((int*)*extra)=newXactIsoLevel;
@@ -642,7 +642,7 @@ check_transaction_deferrable(bool *newval, void **extra, GucSource source)
642642
bool
643643
check_random_seed(double*newval,void**extra,GucSourcesource)
644644
{
645-
*extra=malloc(sizeof(int));
645+
*extra=top_malloc(sizeof(int));
646646
if (!*extra)
647647
return false;
648648
/* Arm the assign only if source of value is an interactive SET */
@@ -739,7 +739,7 @@ check_client_encoding(char **newval, void **extra, GucSource source)
739739
/*
740740
* Save the encoding's ID in *extra, for use by assign_client_encoding.
741741
*/
742-
*extra=malloc(sizeof(int));
742+
*extra=top_malloc(sizeof(int));
743743
if (!*extra)
744744
return false;
745745
*((int*)*extra)=encoding;
@@ -829,7 +829,7 @@ check_session_authorization(char **newval, void **extra, GucSource source)
829829
ReleaseSysCache(roleTup);
830830

831831
/* Set up "extra" struct for assign_session_authorization to use */
832-
myextra= (role_auth_extra*)malloc(sizeof(role_auth_extra));
832+
myextra= (role_auth_extra*)top_malloc(sizeof(role_auth_extra));
833833
if (!myextra)
834834
return false;
835835
myextra->roleid=roleid;
@@ -916,7 +916,7 @@ check_role(char **newval, void **extra, GucSource source)
916916
}
917917

918918
/* Set up "extra" struct for assign_role to use */
919-
myextra= (role_auth_extra*)malloc(sizeof(role_auth_extra));
919+
myextra= (role_auth_extra*)top_malloc(sizeof(role_auth_extra));
920920
if (!myextra)
921921
return false;
922922
myextra->roleid=roleid;

‎src/backend/libpq/pqcomm.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,15 @@ pq_init(void)
227227
AddWaitEventToSet(FeBeWaitSet,WL_POSTMASTER_DEATH,-1,NULL,NULL);
228228
}
229229

230+
voidpq_finalize()
231+
{
232+
if (FeBeWaitSet)
233+
{
234+
FreeWaitEventSet(FeBeWaitSet);
235+
FeBeWaitSet=NULL;
236+
}
237+
}
238+
230239
/* --------------------------------
231240
*socket_comm_reset - reset libpq during error recovery
232241
*
@@ -298,6 +307,7 @@ socket_close(int code, Datum arg)
298307
* We do set sock to PGINVALID_SOCKET to prevent any further I/O,
299308
* though.
300309
*/
310+
close(MyProcPort->sock);
301311
MyProcPort->sock=PGINVALID_SOCKET;
302312
}
303313
}

‎src/backend/postmaster/postmaster.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,8 +1716,8 @@ ServerLoop(void)
17161716
* in this process
17171717
*/
17181718
StreamClose(port->sock);
1719-
ConnFree(port);
17201719
#endif
1720+
ConnFree(port);
17211721
}
17221722
}
17231723
}
@@ -3096,7 +3096,7 @@ reaper(ThreadContext* ctx)
30963096
PG_SETMASK(&UnBlockSig);
30973097

30983098
free(ctx);
3099-
3099+
31003100
errno=save_errno;
31013101
}
31023102

@@ -3993,6 +3993,14 @@ static void thread_cleanup(void* arg)
39933993
{
39943994
ThreadContext*ctx= (ThreadContext*)arg;
39953995
intrc;
3996+
3997+
CleanupLatchSupport();
3998+
pq_finalize();
3999+
4000+
/* Release memory context for this thread */
4001+
MemoryContextReset(TopMemoryContext);
4002+
free(TopMemoryContext);
4003+
39964004
pthread_mutex_lock(&teminated_queue_mutex);
39974005
ctx->next=terminated_queue;
39984006
terminated_queue=ctx;
@@ -4051,6 +4059,8 @@ bool create_thread(pthread_t* t, thread_proc_t thread_proc, void* port)
40514059
save_backend_variables(ctx);
40524060
if (port!=NULL) {
40534061
memcpy(&ctx->port,port,sizeof(Port));
4062+
}else {
4063+
ctx->port.sock=PGINVALID_SOCKET;
40544064
}
40554065
ctx->thread_proc=thread_proc;
40564066
pthread_attr_init(&attr);

‎src/backend/regex/regc_pg_locale.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -738,8 +738,8 @@ store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
738738
if (pcc->cv.nranges >=pcc->cv.rangespace)
739739
{
740740
pcc->cv.rangespace *=2;
741-
newchrs= (chr*)realloc(pcc->cv.ranges,
742-
pcc->cv.rangespace*sizeof(chr)*2);
741+
newchrs= (chr*)top_realloc(pcc->cv.ranges,
742+
pcc->cv.rangespace*sizeof(chr)*2);
743743
if (newchrs==NULL)
744744
return false;
745745
pcc->cv.ranges=newchrs;
@@ -754,8 +754,8 @@ store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
754754
if (pcc->cv.nchrs >=pcc->cv.chrspace)
755755
{
756756
pcc->cv.chrspace *=2;
757-
newchrs= (chr*)realloc(pcc->cv.chrs,
758-
pcc->cv.chrspace*sizeof(chr));
757+
newchrs= (chr*)top_realloc(pcc->cv.chrs,
758+
pcc->cv.chrspace*sizeof(chr));
759759
if (newchrs==NULL)
760760
return false;
761761
pcc->cv.chrs=newchrs;
@@ -794,17 +794,17 @@ pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
794794
/*
795795
* Nope, so initialize some workspace ...
796796
*/
797-
pcc= (pg_ctype_cache*)malloc(sizeof(pg_ctype_cache));
797+
pcc= (pg_ctype_cache*)top_malloc(sizeof(pg_ctype_cache));
798798
if (pcc==NULL)
799799
returnNULL;
800800
pcc->probefunc=probefunc;
801801
pcc->collation=pg_regex_collation;
802802
pcc->cv.nchrs=0;
803803
pcc->cv.chrspace=128;
804-
pcc->cv.chrs= (chr*)malloc(pcc->cv.chrspace*sizeof(chr));
804+
pcc->cv.chrs= (chr*)top_malloc(pcc->cv.chrspace*sizeof(chr));
805805
pcc->cv.nranges=0;
806806
pcc->cv.rangespace=64;
807-
pcc->cv.ranges= (chr*)malloc(pcc->cv.rangespace*sizeof(chr)*2);
807+
pcc->cv.ranges= (chr*)top_malloc(pcc->cv.rangespace*sizeof(chr)*2);
808808
if (pcc->cv.chrs==NULL||pcc->cv.ranges==NULL)
809809
gotoout_of_memory;
810810
pcc->cv.cclasscode=cclasscode;
@@ -879,29 +879,29 @@ pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
879879
*/
880880
if (pcc->cv.nchrs==0)
881881
{
882-
free(pcc->cv.chrs);
882+
top_free(pcc->cv.chrs);
883883
pcc->cv.chrs=NULL;
884884
pcc->cv.chrspace=0;
885885
}
886886
elseif (pcc->cv.nchrs<pcc->cv.chrspace)
887887
{
888-
newchrs= (chr*)realloc(pcc->cv.chrs,
889-
pcc->cv.nchrs*sizeof(chr));
888+
newchrs= (chr*)top_realloc(pcc->cv.chrs,
889+
pcc->cv.nchrs*sizeof(chr));
890890
if (newchrs==NULL)
891891
gotoout_of_memory;
892892
pcc->cv.chrs=newchrs;
893893
pcc->cv.chrspace=pcc->cv.nchrs;
894894
}
895895
if (pcc->cv.nranges==0)
896896
{
897-
free(pcc->cv.ranges);
897+
top_free(pcc->cv.ranges);
898898
pcc->cv.ranges=NULL;
899899
pcc->cv.rangespace=0;
900900
}
901901
elseif (pcc->cv.nranges<pcc->cv.rangespace)
902902
{
903-
newchrs= (chr*)realloc(pcc->cv.ranges,
904-
pcc->cv.nranges*sizeof(chr)*2);
903+
newchrs= (chr*)top_realloc(pcc->cv.ranges,
904+
pcc->cv.nranges*sizeof(chr)*2);
905905
if (newchrs==NULL)
906906
gotoout_of_memory;
907907
pcc->cv.ranges=newchrs;
@@ -921,10 +921,10 @@ pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
921921
*/
922922
out_of_memory:
923923
if (pcc->cv.chrs)
924-
free(pcc->cv.chrs);
924+
pfree(pcc->cv.chrs);
925925
if (pcc->cv.ranges)
926-
free(pcc->cv.ranges);
927-
free(pcc);
926+
pfree(pcc->cv.ranges);
927+
pfree(pcc);
928928

929929
returnNULL;
930930
}

‎src/backend/storage/file/fd.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2668,7 +2668,16 @@ AtEOXact_Files(void)
26682668
staticvoid
26692669
AtProcExit_Files(intcode,Datumarg)
26702670
{
2671+
inti;
26712672
CleanupTempFiles(true);
2673+
2674+
for (i=1;i<SizeVfdCache;i++)
2675+
{
2676+
if (VfdCache[i].fileName!=NULL)
2677+
FileClose(i);
2678+
}
2679+
free(VfdCache);
2680+
free(allocatedDescs);
26722681
}
26732682

26742683
/*

‎src/backend/storage/ipc/latch.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include"port/atomics.h"
4949
#include"portability/instr_time.h"
5050
#include"postmaster/postmaster.h"
51+
#include"storage/fd.h"
5152
#include"storage/latch.h"
5253
#include"storage/pmsignal.h"
5354
#include"storage/shmem.h"
@@ -213,6 +214,13 @@ InitializeLatchSupport(void)
213214
#endif
214215
}
215216

217+
218+
voidCleanupLatchSupport()
219+
{
220+
close(selfpipe_readfd);
221+
close(selfpipe_writefd);
222+
}
223+
216224
/*
217225
* Initialize a process-local latch.
218226
*/

‎src/backend/storage/ipc/procarray.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,7 +1051,7 @@ TransactionIdIsInProgress(TransactionId xid)
10511051
*/
10521052
intmaxxids=RecoveryInProgress() ?TOTAL_MAX_CACHED_SUBXIDS :arrayP->maxProcs;
10531053

1054-
xids= (TransactionId*)malloc(maxxids*sizeof(TransactionId));
1054+
xids= (TransactionId*)top_malloc(maxxids*sizeof(TransactionId));
10551055
if (xids==NULL)
10561056
ereport(ERROR,
10571057
(errcode(ERRCODE_OUT_OF_MEMORY),
@@ -1539,14 +1539,14 @@ GetSnapshotData(Snapshot snapshot)
15391539
* we are in recovery, see later comments.
15401540
*/
15411541
snapshot->xip= (TransactionId*)
1542-
malloc(GetMaxSnapshotXidCount()*sizeof(TransactionId));
1542+
top_malloc(GetMaxSnapshotXidCount()*sizeof(TransactionId));
15431543
if (snapshot->xip==NULL)
15441544
ereport(ERROR,
15451545
(errcode(ERRCODE_OUT_OF_MEMORY),
15461546
errmsg("out of memory")));
15471547
Assert(snapshot->subxip==NULL);
15481548
snapshot->subxip= (TransactionId*)
1549-
malloc(GetMaxSnapshotSubxidCount()*sizeof(TransactionId));
1549+
top_malloc(GetMaxSnapshotSubxidCount()*sizeof(TransactionId));
15501550
if (snapshot->subxip==NULL)
15511551
ereport(ERROR,
15521552
(errcode(ERRCODE_OUT_OF_MEMORY),
@@ -1963,7 +1963,7 @@ GetRunningTransactionData(void)
19631963
* First call
19641964
*/
19651965
CurrentRunningXacts->xids= (TransactionId*)
1966-
malloc(TOTAL_MAX_CACHED_SUBXIDS*sizeof(TransactionId));
1966+
top_malloc(TOTAL_MAX_CACHED_SUBXIDS*sizeof(TransactionId));
19671967
if (CurrentRunningXacts->xids==NULL)
19681968
ereport(ERROR,
19691969
(errcode(ERRCODE_OUT_OF_MEMORY),
@@ -2574,7 +2574,7 @@ GetConflictingVirtualXIDs(TransactionId limitXmin, Oid dbOid)
25742574
if (vxids==NULL)
25752575
{
25762576
vxids= (VirtualTransactionId*)
2577-
malloc(sizeof(VirtualTransactionId)* (arrayP->maxProcs+1));
2577+
top_malloc(sizeof(VirtualTransactionId)* (arrayP->maxProcs+1));
25782578
if (vxids==NULL)
25792579
ereport(ERROR,
25802580
(errcode(ERRCODE_OUT_OF_MEMORY),

‎src/backend/utils/adt/datetime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4542,7 +4542,7 @@ ConvertTimeZoneAbbrevs(struct tzEntry *abbrevs, int n)
45424542
}
45434543

45444544
/* Alloc the result ... */
4545-
tbl=malloc(tbl_size);
4545+
tbl=top_malloc(tbl_size);
45464546
if (!tbl)
45474547
returnNULL;
45484548

‎src/backend/utils/adt/regexp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ RE_compile_and_cache(text *text_re, int cflags, Oid collation)
210210
* out-of-memory. The Max() is because some malloc implementations return
211211
* NULL for malloc(0).
212212
*/
213-
re_temp.cre_pat=malloc(Max(text_re_len,1));
213+
re_temp.cre_pat=top_malloc(Max(text_re_len,1));
214214
if (re_temp.cre_pat==NULL)
215215
{
216216
pg_regfree(&re_temp.cre_re);
@@ -232,7 +232,7 @@ RE_compile_and_cache(text *text_re, int cflags, Oid collation)
232232
--num_res;
233233
Assert(num_res<MAX_CACHED_RES);
234234
pg_regfree(&re_array[num_res].cre_re);
235-
free(re_array[num_res].cre_pat);
235+
top_free(re_array[num_res].cre_pat);
236236
}
237237

238238
if (num_res>0)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp