This post is about manually calling and freeing a shared latch. Credits should go to Andrey Nikolaev, who has this covered in his presentation which was presented at UKOUG Tech 15. I am very sorry to see I did miss it.
Essentially, if you follow my Oracle 12 and shared latches part 2 blogpost, which is about shared latches, I showed how to get a shared latch:
SQL> oradebug setmypid Statement processed. SQL> oradebug call ksl_get_shared_latch 0x94af8768 1 0 2303 16 Function returned 1
Which works okay, but leaves a bit of a mess when freed:
SQL> oradebug call kslfre 0x93FC02B8 ORA-03113: end-of-file on communication channel ORA-24323: value not allowed
(please read part 2 to get the full story on how to set it and how to use it for investigations)
Andrey Nikolaev shows there is another function, kslgetsl_w(latch address, wait, why, where, mode), which can be used to get a shared latch (alike the function ksl_get_shared_latch) but without the oradebug session blowing up in your face:
SQL> oradebug call kslgetsl_w 0x93faec78 1 0 2329 16 Function returned 1
And after testing the latch:
SQL> oradebug call kslfre 0x93faec78 Function returned 1
However, when querying v$latchholder from the same session as the oradebug call, an ORA-600 is thrown:
SQL> select * from v$latchholder; select * from v$latchholder * ERROR at line 1: ORA-00600: internal error code, arguments: , [0x095306210], , , [session idle bit], , [0x093FAEC78], [cache buffers chains], , , , 
After which the latch is freed.
The error message strongly hints at the latches gotten in an incompatible order. The session idle bit seems to be a latch gotten to indicate the session state switching from active to inactive (according to an article here), and indeed holding a cache buffer chains latch, a session should not switch state at all. However, a second sysdba session can be used to query v$latchholder.
Some very limited investigations show the function is kslgetsl_w is not used by the database itself, which rather uses ksl_get_shared_latch. Again, the investigation was very limited, there might be situations this function is used.
The reason for this blogpost is to point out an alternative for using the ksl_get_shared_latch function manually for doing investigation. This post also shows that you should not change or play with internals on production systems, because it might lead to all kinds of additional behaviour or plain errors, for which the ORA-600 is a good example.