Skip to content

SmartCoreAI drivers/components/code is not thread-safe. #4

@SkybuckFlying

Description

@SkybuckFlying

This could be a serious issue, prevents using multiple AI models at the same time.

Should be looked at....

Possible issue:

The user is still experiencing access violations, this time in TAIGeminiDriver.GenerateContentAsync. The stack trace shows it's happening in the SmartCoreAI library's async task execution.

The issue is that even though I removed the parallel threading, the SmartCoreAI components themselves use async operations internally via Delphi's task/thread pool. When we call Chat.Chat(), it triggers an async HTTP request, and when we free the components, those async operations may still be running.

The problem is:

We call Chat.Chat('Hello, respond with OK')
This starts an async HTTP operation in the thread pool
We wait for the event with Done.WaitFor(30000)
If it times out or completes, we immediately free all objects in the finally block
But the async operation in the thread pool might still be trying to access those freed objects
The solution is to ensure we wait long enough after the operation completes/times out before freeing the objects, OR we need to properly cancel/shutdown the async operations before freeing.

Since SmartCoreAI uses Driver.SynchronizeEvents := False (async mode), we need to be very careful about cleanup. Let me add a small delay after operations complete before freeing, to give the thread pool time to finish.

Alternatively, we could set SynchronizeEvents := True to make the operations synchronous, which would eliminate the threading issues entirely.

Let me try setting SynchronizeEvents to True to make operations synchronous.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions