On Mon, Nov 11, 2019 at 05:37:37PM +0000, Simon Slavin wrote: > On 11 Nov 2019, at 5:13pm, Jukka Marin <jukka.ma...@jmp.fi> wrote: > > > The main process first opens the databases and checks that their > > version matches that of the software and if not, the databases are > > closed and initialized by running a script. > > > > After closing the databases, main process forks the children and > > all processes (including main process) open the databases and use > > their own connections. > > > > What I was trying to ask was this: If any of the children dies > > (a bug in the code), main process will restart the child. At > > this point, the main process has the databases open, so the new > > child receives the connections as well. What should I do now? > > Okay, that gives us enough information to work with. > > The conservative way to do it is to have the main process close the > connection before forking and open it again. Then, of course, the child > processes make their own connections. > > But I don't think that's necessary. A child process can have access to the > main process' database connection but ignore it. So I think the main process > can fork without closing its connection. Then each child can never use that > one but instead make its own.
Okay, that's what I was hoping for. > Of course, every one of these connections needs to set a timeout. And every > call to the SQLite3 library needs to check its result code and make sure it > is getting SQLITE_OK (or, for queries, SQLITE_DONE etc.). Yes, I'm doing all this, > > Should the child close the databases before opening them again? > > Will this close the databases for the main process as well? > > As you suspected, closing the connection releases both memory structures and > file handles. Anything that tries to use that connection will then fail > because it has no idea what it's talking to. Ok. > What puzzles me is this: you're getting "database malformed" and nothing > you've described justifies this. Assuming that this isn't just one old > database which is genuinely corrupt, but that you are using a fresh uncorrupt > database each time, you seem to have a genuine bug in your code. That was happening before I changed my code to open the databases in the children. Originally, only main process opened the databases and the children "inherited" the connections. I was wondering if this was okay, but I didn't see a warning in the SQLite docs, so.. I never found the "how to corrupt your database" manual on my own ;-) I came back to the mailing list when I noticed the above "problem" of a child dying and needing to respawn it while the main process already has the databases open. So I guess it's safe now that all the children open the databases by themselves. Thanks to all who responded! -jm _______________________________________________ sqlite-users mailing list sqlite-users@mailinglists.sqlite.org http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users