Change in osmo-hlr[master]: src/db.c: fix: make sure the database is properly closed

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Vadim Yanitskiy gerrit-no-reply at lists.osmocom.org
Mon Jul 30 13:31:20 UTC 2018


Vadim Yanitskiy has uploaded this change for review. ( https://gerrit.osmocom.org/10238


Change subject: src/db.c: fix: make sure the database is properly closed
......................................................................

src/db.c: fix: make sure the database is properly closed

Thanks to ASAN, it was discovered that some part of heap
is not released on exit:

  ==19736==ERROR: LeakSanitizer: detected memory leaks

  Indirect leak of 94616 byte(s) in 214 object(s) allocated from:
    #0 0x4e05c6  (/home/wmn/osmocom/osmo-hlr/src/osmo-hlr+0x4e05c6)
    #1 0x7f9b01061dc6  (/usr/lib/x86_64-linux-gnu/libsqlite3.so.0+0x33dc6)

  Indirect leak of 1160 byte(s) in 1 object(s) allocated from:
    #0 0x4e097d  (/home/wmn/osmocom/osmo-hlr/src/osmo-hlr+0x4e097d)
    #1 0x7f9b01061d58  (/usr/lib/x86_64-linux-gnu/libsqlite3.so.0+0x33d58)

  SUMMARY: AddressSanitizer: 95776 byte(s) leaked in 215 allocation(s).

After a long investigation, it was figured out that *sqlite never
closes the database* due to 'unfinalized statements or unfinished
backups'.

The problem was in db_bootstrap(), where several statements were
prepared, but not finalized in loop. This was also the reason of
*.db-shm / *.db-wal files remaining after the program is closed,
and the reason of the following message

  db.c:77 (283) recovered 18 frames from WAL file *.db-wal

Let's fix this and stop ignoring the result of sqlite3_close().

Change-Id: Ibe620d7723b1947d4f60f820bd18435ad0193112
Related: OS#3434
---
M src/db.c
1 file changed, 10 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-hlr refs/changes/38/10238/1

diff --git a/src/db.c b/src/db.c
index cc06ee6..2d1bdde 100644
--- a/src/db.c
+++ b/src/db.c
@@ -173,12 +173,20 @@
 void db_close(struct db_context *dbc)
 {
 	unsigned int i;
+	int rc;
 
 	for (i = 0; i < ARRAY_SIZE(dbc->stmt); i++) {
 		/* it is ok to call finalize on NULL */
 		sqlite3_finalize(dbc->stmt[i]);
 	}
-	sqlite3_close(dbc->db);
+
+	/* Ask sqlite3 to close DB */
+	rc = sqlite3_close(dbc->db);
+	if (rc != SQLITE_OK) { /* Make sure it's actually closed! */
+		LOGP(DDB, LOGL_ERROR, "Couldn't close database: (rc=%d) %s\n",
+			rc, sqlite3_errmsg(dbc->db));
+	}
+
 	talloc_free(dbc);
 }
 
@@ -200,6 +208,7 @@
 		/* execute the statement */
 		rc = sqlite3_step(stmt);
 		db_remove_reset(stmt);
+		sqlite3_finalize(stmt);
 		if (rc != SQLITE_DONE) {
 			LOGP(DDB, LOGL_ERROR, "Cannot bootstrap database: SQL error: (%d) %s,"
 			     " during stmt '%s'",

-- 
To view, visit https://gerrit.osmocom.org/10238
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-hlr
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibe620d7723b1947d4f60f820bd18435ad0193112
Gerrit-Change-Number: 10238
Gerrit-PatchSet: 1
Gerrit-Owner: Vadim Yanitskiy <axilirator at gmail.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180730/1c73a218/attachment.htm>


More information about the gerrit-log mailing list