Repository navigation
Feature/optimize 01 Fix thread-unsafe LinkedList access in OpenSSLConnector pending buffer lists - #11
Conversation
…r lists (GML-1187)
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Per-list locks still allow concurrent RunSSL executions against shared OpenSSL state.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Adds synchronization around pending OpenSSL read/write buffer lists.
Changes:
- Introduces separate read/write list locks.
- Protects queue counts and mutations.
| File | Description |
|---|---|
OpenSSLConnector.cs |
Synchronizes pending buffer-list access. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| private readonly object m_readListLock = new object(); | ||
| private readonly object m_writeListLock = new object(); |
There was a problem hiding this comment.
A possible solution in the class OpenSSLConnector
Add one connector-wide gate object, e.g. m_sslLock, and make every entry point that drives a protocol step serialize on it while keeping the per-list locks for producer AppendBuffer:
private readonly object m_sslGate = new object();
public void Write(byte[] pData, int dataLen)
{
lock (m_sslGate)
{
AppendBuffer(m_pendingWriteList, new DataBuffer(pData, dataLen));
RunSSL();
}
}
public void ReadCompleted(byte[] pData, int dataLen)
{
lock (m_sslGate)
{
AppendBuffer(m_pendingReadList, new DataBuffer(pData, dataLen));
RunSSL();
}
}Because Monitor is re-entrant, same-thread nesting of RunSSL (a real possibility given the callback-driven data flow) is fine; the gate just prevents a second thread from entering the whole processing path. Keep AppendBuffer's internal list lock only for any producer that appends without driving RunSSL.
Because Monitor is re-entrant, same-thread nesting of RunSSL (a real possibility given the callback-driven data flow) is fine; the gate just prevents a second thread from entering the whole processing path. Keep AppendBuffer's internal list lock only for any producer that appends without driving RunSSL.
|
My assessment after reviewing the diff, discussion, and caller paths at The added I would still address these points before describing the connector as thread-safe:
I agree with the direction of the fix. The original two-thread processing issue is addressed in the latest commit; the remaining items concern complete SSL lifetime/access protection and callback reentrancy. I recommend targeted regression tests for simultaneous write/read processing, teardown during a write, and callback re-entry. This assessment is based on code inspection; I have not run a native OpenSSL concurrency test or PLC integration test. |
|
Merged in dependency order with the subsequent PRs. Follow-up commit 7915bcb protects exporter access and disposal with the connector gate, prevents recursive SSL processing from callbacks, defers native cleanup until processing returns, and rejects operations after disposal. Five native OpenSSL regression tests cover callback re-entry, callback disposal, concurrent exporter/disposal calls, and use after disposal. The final integrated offline suite passes 290 tests on .NET 8 and 285 on .NET Framework 4.8; no live PLC test was run. |

No description provided.