-
-
Notifications
You must be signed in to change notification settings - Fork 391
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
Sled database is not really closed after dropping sled::Db
.
#1234
Comments
Is this a sled issue or does this have to do with normal linux fd reaping behavior? |
What do you mean by "fd reaping"? Releasing flock on descriptor close or closing descriptors on process exit? |
Does Sled use any global singetons under the hood by the way? |
There is a choice about whether to fail if the advisory lock cannot be taken immediately, or to block indefinitely. When testing sled, we use the blocking approach because fd reaping is a lazy process and can take a few moments, and under test we know that we are the only ones using a database. But when running outside of test mode, we do not block indefinitely, because that would provide the user with no actionable information about what the problem is. The code you've listed in the example is expected to fail often, because reaping is lazy. Please keep the database open while using it, and avoid using it from multiple processes. The lazy reaping should almost always happen before you can get around to opening the database again in a new process. |
How do I wait for database to be free for reuse by external tools? Maybe there should be some Also
From a distance Sled looks like a file-backed thread-safe |
For example, how would a correct code for closing the database, then causing underlying filesystem unmount (and storage eject) would look like? |
Sled closes the file before drop returns. The FD reaping process @spacejam refers to is a behavior of the Linux kernel. There isn't anything else sled can do during shutdown to affect this. I have had need to repeatedly close and reopen a database repeatedly for crash testing, and this can be done with a blocking flock() call on the file before opening the database. (via fs2's FileExt::lock_exclusive()) However, I would not recommend this for normal use. Unnecessarily closing and reopening a database is expensive, as each time it will perform database recovery, reading the snapshot/log to load metadata. |
If so,
But I don't see a |
Simplified repro: fn main() {
let db: sled::Db = sled::open("/home/vi/code/ddb/db.sled").unwrap();
db.insert(b"qqq2", format!("{:?}",std::time::Instant::now()).as_bytes()).unwrap();
db.flush().unwrap();
drop(db);
eprintln!("Finished closing the database?");
std::thread::sleep(std::time::Duration::from_secs(1000000));
} clearly shows that database's primary file descriptor (3) is closed way after it returns from the strace run:
|
I cannot reproduce your results with sled 0.34.6. Perhaps you are using an older version with a bug in a Drop impl?
|
Shall I try with Sled from master branch? |
Note: the problem is not reproducible with initially empty database (neither with 0.34.6 nor with master commit aa3fcf3). I use a database that takes about 55MB on disk. |
Again, this is expected behavior due to the way that advisory locking works on linux. Even though there may be a backing IO thread that holds the file open, advisory locking does not always immediately release the file when a process calls close. Especially on other systems like macos, there are a lot of reasons why this would not immediately be openable without using a blocking lock request, which is not an option for us because it would cause locking that the user would get no feedback about, and a lot of issues would be opened about the database stalling for no reason. Again, please do not rapidly open and close the same database. This is not supported behavior. Keep a single open database. |
Example code snippet (non-standalone):
sled::Db
instances.drop
, then failsflock
on the next open.The text was updated successfully, but these errors were encountered: