This discussion is archived
1 Reply Latest reply: Oct 7, 2013 12:00 AM by c6ae0fc6-e0c3-494d-9a53-09f8ec293b5e RSS

Bad File Descriptor ERROR

c6ae0fc6-e0c3-494d-9a53-09f8ec293b5e Newbie
Currently Being Moderated

Hi Friends,

 

Here is my scenario:

 

while (INFINITE_LOOP) {

     1. Create eraser.db DB with enabling environment & no transaction in dir: /DB_DIR

     2. Create expire.db DB without enabling environment & no transaction in same dir: /DB_DIR

     3. Perform add - remove record operations on both dbs.

     3. Close both dbs & environment

     4. Remove entire directory : /DB_DIR (using posix rmdir & unlink apis)

     5. sleep for 2 mins. (This is for stress testing... actual sleep value is 24 hrs)

}

 

This while loop works for few iterations. But after few iterations I start getting below errors:

 

seek: 0: (0 * 4096) + 0: Bad file descriptor

eraser.db: write failed for page 0

eraser.db: unable to flush page: 0

seek: 4096: (1 * 4096) + 0: Bad file descriptor

eraser.db: write failed for page 1

eraser.db: unable to flush page: 1

seek: 8192: (2 * 4096) + 0: Bad file descriptor

eraser.db: write failed for page 2

eraser.db: unable to flush page: 2

 

Once these errors start occuring. All the db operations start failing for all the following iterations.

Now I have to stop my program & restart, then only db starts working as expected.

 

Kindly suggest the root cause of the problem.

 

Regards,
~ Ashish K.

  • 1. Re: Bad File Descriptor ERROR
    c6ae0fc6-e0c3-494d-9a53-09f8ec293b5e Newbie
    Currently Being Moderated

    Hi Friends,

    The above mentioned issues was not related to BDB. Finally I found "opened once, but closed twice" file descriptor issue in my multi threaded code.In our code a socket descriptor was getting closed twice. After few iterations of running, this problem was unintentionally leading to closing of one of the internal file descriptor of BDB. Once I fixed that problem. BDB started working as expected.

     

    For those who are unfamiliar to closing twice issue in multi threaded code read below:

     

    foo ()

    {

        fd = open();

        // some processing.

        close (fd);

        // some processing.

        close (fd);

    }

     

    The above code will work smoothly for a single threaded application. But in our case of multithreading, if a thread "X" call open(), when thread "Y" is in between the two close() calls, then in that case, thread "X" will get the same fd for open which thread "Y" was using (since open always return the smallest unused descriptor). File descriptors are shared by all the threads. So when thread "Y" carries out its second close(), it actually closes the file descriptor of thread "X", which was valid & in use.

     

    This is what precisely happening with my code.

     

    TIP: Whenever your multi threaded code, starts giving BDB errors like "Bad file descriptor" first check if there is a place in code where you are "opening once but closing twice" any file descriptor.

     

    Regards,
    ~ Ashish K.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points