Skip to content

Commit

Permalink
Fix two grammar callbacks that silently fail out-of-process (#210)
Browse files Browse the repository at this point in the history
The grammar recognition and hypothesis callbacks have been silently
failing on Windows 10 when Natlink is used in a separate Python
process.  Holding the GIL during the callbacks and making a minor
change in the natConnect() function fixes this problem.
  • Loading branch information
drmfinlay authored Sep 26, 2024
1 parent 62611ac commit 5d61154
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion NatlinkSource/DragonCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,9 @@ BOOL CDragonCode::natConnect( IServiceProvider * pIDgnSite, BOOL bUseThreads )
{
PyThreadState * threadStateSave = PyThreadState_Swap( NULL );
assert( threadStateSave != NULL );
m_pThreadState = PyThreadState_New( threadStateSave->interp );
// DF: Simple assignment works better than PyThreadState_New().
//m_pThreadState = PyThreadState_New( threadStateSave->interp );
m_pThreadState = threadStateSave;
PyThreadState_Swap( threadStateSave );
}

Expand Down
6 changes: 6 additions & 0 deletions NatlinkSource/GrammarObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ STDMETHODIMP CSRGramNotifySink::PhraseFinish(

if( m_pParent )
{
// This call requires Python's GIL to be held.
PyGILState_STATE gstate = PyGILState_Ensure();
m_pParent->PhraseFinish( dwFlags, pSRPhrase, pIUnknown );
PyGILState_Release( gstate );
}

return S_OK;
Expand All @@ -136,7 +139,10 @@ STDMETHODIMP CSRGramNotifySink::PhraseHypothesis(

if( m_pParent )
{
// This call requires Python's GIL to be held.
PyGILState_STATE gstate = PyGILState_Ensure();
m_pParent->PhraseHypothesis( dwFlags, pSRPhrase );
PyGILState_Release( gstate );
}

return S_OK;
Expand Down

0 comments on commit 5d61154

Please sign in to comment.