Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[YSQL] Memory leak in org.yb.pgsql.TestPgWithoutWaitQueuesIsolationRegress.runPgTransparentRestartsRegressTest #15612

Closed
tvesely opened this issue Jan 9, 2023 · 0 comments
Assignees
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug priority/medium Medium priority issue

Comments

@tvesely
Copy link
Contributor

tvesely commented Jan 9, 2023

Jira Link: DB-4995

Description

There appears to be a memory leak in pg_isolation that has been detected by ASAN.

+ =================================================================
+ ==18113==ERROR: LeakSanitizer: detected memory leaks
+·
+ Direct leak of 2048 byte(s) in 1 object(s) allocated from:
+     #0 0x55f21c2846d6 in realloc /opt/yb-build/llvm/yb-llvm-v15.0.3-yb-1-1667030060-0b8d1183-almalinux8-x86_64-build/src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:85:3
+     #1 0x55f21c2cb0a9 in pg_realloc ${YB_SRC_ROOT}/src/postgres/src/common/../../../../../src/postgres/src/common/fe_memutils.c:72:8
+     #2 0x55f21c2c27b2 in addlitchar ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specscanner.l:116:12
+     #3 0x55f21c2c27b2 in spec_yylex ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specscanner.l:90:6
+     #4 0x55f21c2be95f in spec_yyparse ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specparse.c:1190:16
+     #5 0x55f21c2c5492 in main ${YB_SRC_ROOT}/src/postgres/src/test/isolation/../../../../../../src/postgres/src/test/isolation/isolationtester.c:116:2
+     #6 0x7f027726ed84 in __libc_start_main (/lib64/libc.so.6+0x3ad84) (BuildId: d18afae5244bc9c85026bd7d64b276d51b452d93)
+·
+ Objects leaked above:
+ 0x61d000000080 (2048 bytes)
+·
+ SUMMARY: AddressSanitizer: 2048 byte(s) leaked in 1 allocation(s).
@tvesely tvesely added area/ysql Yugabyte SQL (YSQL) status/awaiting-triage Issue awaiting triage labels Jan 9, 2023
@yugabyte-ci yugabyte-ci added kind/bug This issue is a bug priority/medium Medium priority issue labels Jan 9, 2023
@yugabyte-ci yugabyte-ci removed the status/awaiting-triage Issue awaiting triage label Jan 22, 2023
tvesely added a commit that referenced this issue Mar 27, 2023
Summary:
This commit addresses a memory leak in the pg_isolation_regress test suite
detected by ASAN:

```
 ==18113==ERROR: LeakSanitizer: detected memory leaks

 Direct leak of 2048 byte(s) in 1 object(s) allocated from:
     #0 0x55f21c2846d6 in realloc /opt/yb-build/llvm/yb-llvm-v15.0.3-yb-1-1667030060-0b8d1183-almalinux8-x86_64-build/src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:85:3
     #1 0x55f21c2cb0a9 in pg_realloc ${YB_SRC_ROOT}/src/postgres/src/common/../../../../../src/postgres/src/common/fe_memutils.c:72:8
     #2 0x55f21c2c27b2 in addlitchar ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specscanner.l:116:12
     #3 0x55f21c2c27b2 in spec_yylex ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specscanner.l:90:6
     #4 0x55f21c2be95f in spec_yyparse ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specparse.c:1190:16
     #5 0x55f21c2c5492 in main ${YB_SRC_ROOT}/src/postgres/src/test/isolation/../../../../../../src/postgres/src/test/isolation/isolationtester.c:116:2
     #6 0x7f027726ed84 in __libc_start_main (/lib64/libc.so.6+0x3ad84) (BuildId: d18afae5244bc9c85026bd7d64b276d51b452d93)

 Objects leaked above:
 0x61d000000080 (2048 bytes)

 SUMMARY: AddressSanitizer: 2048 byte(s) leaked in 1 allocation(s).
```

The memory leak is caused by the litbuf variable in specscanner.l, which is
being reallocated in the addlitchar function but not freed properly. The leak
occurs in two ways:

  1. litbuf is allocated multiple times without being freed, leading to memory
     leaks.
  2. litbuf is not properly released after spec_yyparse() is executed.

To resolve these issues, the following changes have been made:

  1. litbuf memory allocation is now initialized only when it is NULL, preventing
     multiple allocations and enabling the buffer to be reused.
  2. A spec_scanner_finish() function is introduced to clean up the allocated
    memory. This function frees the litbuf memory after spec_yyparse() is
    executed, preventing the memory leak.

Test Plan:
Run pg_isolation_regress to confirm that the memory leak is resolved:

/yb_build.sh asan --java-test 'org.yb.pgsql.TestPgWithoutWaitQueuesIsolationRegress' -n 100 --tp 1

Reviewers: bogdan, pjain

Reviewed By: pjain

Subscribers: smishra, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D23912
@tvesely tvesely closed this as completed Mar 27, 2023
premkumr pushed a commit to premkumr/yugabyte-db that referenced this issue Apr 10, 2023
Summary:
This commit addresses a memory leak in the pg_isolation_regress test suite
detected by ASAN:

```
 ==18113==ERROR: LeakSanitizer: detected memory leaks

 Direct leak of 2048 byte(s) in 1 object(s) allocated from:
     #0 0x55f21c2846d6 in realloc /opt/yb-build/llvm/yb-llvm-v15.0.3-yb-1-1667030060-0b8d1183-almalinux8-x86_64-build/src/llvm-project/compiler-rt/lib/asan/asan_malloc_linux.cpp:85:3
     #1 0x55f21c2cb0a9 in pg_realloc ${YB_SRC_ROOT}/src/postgres/src/common/../../../../../src/postgres/src/common/fe_memutils.c:72:8
     #2 0x55f21c2c27b2 in addlitchar ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specscanner.l:116:12
     yugabyte#3 0x55f21c2c27b2 in spec_yylex ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specscanner.l:90:6
     yugabyte#4 0x55f21c2be95f in spec_yyparse ${YB_SRC_ROOT}/src/postgres/src/test/isolation/specparse.c:1190:16
     yugabyte#5 0x55f21c2c5492 in main ${YB_SRC_ROOT}/src/postgres/src/test/isolation/../../../../../../src/postgres/src/test/isolation/isolationtester.c:116:2
     yugabyte#6 0x7f027726ed84 in __libc_start_main (/lib64/libc.so.6+0x3ad84) (BuildId: d18afae5244bc9c85026bd7d64b276d51b452d93)

 Objects leaked above:
 0x61d000000080 (2048 bytes)

 SUMMARY: AddressSanitizer: 2048 byte(s) leaked in 1 allocation(s).
```

The memory leak is caused by the litbuf variable in specscanner.l, which is
being reallocated in the addlitchar function but not freed properly. The leak
occurs in two ways:

  1. litbuf is allocated multiple times without being freed, leading to memory
     leaks.
  2. litbuf is not properly released after spec_yyparse() is executed.

To resolve these issues, the following changes have been made:

  1. litbuf memory allocation is now initialized only when it is NULL, preventing
     multiple allocations and enabling the buffer to be reused.
  2. A spec_scanner_finish() function is introduced to clean up the allocated
    memory. This function frees the litbuf memory after spec_yyparse() is
    executed, preventing the memory leak.

Test Plan:
Run pg_isolation_regress to confirm that the memory leak is resolved:

/yb_build.sh asan --java-test 'org.yb.pgsql.TestPgWithoutWaitQueuesIsolationRegress' -n 100 --tp 1

Reviewers: bogdan, pjain

Reviewed By: pjain

Subscribers: smishra, yql

Differential Revision: https://phabricator.dev.yugabyte.com/D23912
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/ysql Yugabyte SQL (YSQL) kind/bug This issue is a bug priority/medium Medium priority issue
Projects
None yet
Development

No branches or pull requests

2 participants