Skip to content

close index file fd only if index is not build via socket#331

Merged
var77 merged 1 commit intomainfrom
varik/fix-index-build-crash
Sep 3, 2024
Merged

close index file fd only if index is not build via socket#331
var77 merged 1 commit intomainfrom
varik/fix-index-build-crash

Conversation

@var77
Copy link
Copy Markdown
Collaborator

@var77 var77 commented Aug 20, 2024

We were calling close syscall on index_file_fd variable for all cases when building the index, but when building index with external socket no index file is used, so index_file_fd is not allocated. This led to undefined behaviour and led to crashes in some systems with errors like:
error: could not seek to end of file "base/5/2610": Bad file descriptor or
PANIC: could not write to log file 00000001000000000000004C at offset 13369344, length 8192: Bad file descriptor

@var77 var77 requested a review from Ngalstyan4 August 20, 2024 12:49
Copy link
Copy Markdown

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This pull request addresses a critical issue in the LanternDB extension, fixing crashes related to improper file descriptor handling during index building.

  • Modified src/hnsw/build.c to close the index file descriptor only when building from an existing file, preventing crashes when using external sockets
  • Added assertion in src/hnsw/build.c to ensure valid file descriptor when building from existing file
  • Updated version from 0.3.2 to 0.3.3 in CMakeLists.txt, reflecting the bug fix
  • Added new SQL update file sql/updates/0.3.2--0.3.3.sql for version migration (contents unavailable for review)

3 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings

include(CheckSymbolExists)

set(LANTERN_VERSION 0.3.2)
set(LANTERN_VERSION 0.3.3)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info: updated version number from 0.3.2 to 0.3.3

Comment on lines 554 to +555
index_file_fd = open(buildstate->index_file_path, O_RDONLY);
assert(index_file_fd > 0);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info: Added assertion to ensure index_file_fd is valid when opened

Comment on lines 598 to +601
munmap_ret = munmap(result_buf, index_file_stat.st_size);
assert(munmap_ret == 0);
LDB_UNUSED(munmap_ret);
close(index_file_fd);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info: Moved close(index_file_fd) inside the conditional block where it's used

LDB_UNUSED(munmap_ret);
close(index_file_fd);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info: Removed unconditional close(index_file_fd) to prevent undefined behavior

@github-actions
Copy link
Copy Markdown

Benchmarks

metric old new pct change
recall (after create) 0.952 0.953 +0.11%
recall (after insert) 0.000 0.000 -
select tps 26694.845 29205.963 +9.41%
select bulk(100) tps 36.678 36.101 -1.58%
select latency (ms) 0.705 ± 1.491𝜎 0.726 ± 1.350𝜎 +2.98%
select bulk(100) latency (ms) 855.498 ± 112.767𝜎 867.356 ± 117.917𝜎 +1.39%
create latency (ms) 391018.657 392931.251 +0.49%
insert tps 441.719 443.936 +0.50%
insert bulk(100) tps 4.753 4.783 +0.64%
insert latency (ms) 71.822 ± 16.181𝜎 71.525 ± 15.995𝜎 -0.41%
insert bulk(100) latency (ms) 6629.397 ± 96.700𝜎 6523.381 ± 149.688𝜎 -1.60%
disk usage (bytes) 8192008192.000 8192008192.000 -

@var77 var77 force-pushed the varik/fix-index-build-crash branch from 35aad5b to 072283a Compare August 20, 2024 13:01
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 20, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@var77 var77 merged commit d85aeae into main Sep 3, 2024
@var77 var77 deleted the varik/fix-index-build-crash branch September 3, 2024 07:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant