Creating a graph and playable in editor context resulting in an error


I'm trying to do a timeline clip visualisation of the effect it's going to cause in the inspector.
Basically we have a base TransformTrack which allows you to perform a set of tweens movements on a number of game objects at a time.
There is no possibility of actually previewing the timeline for scrubbing as none of the items is actually created, they're fed in to the clip at runtime - that is why I basically want to simulate how something will behave and have a small preview window in the inspector of that clip.

I got quite far with this, basically got he working preview window and the whole simulation nicely playing back.
The only issue is that on any code change when we trigger a script compilation I get a

Assembly Cache Should Be Empty

This only happens when I use that inspector and I presume there is an issue as for the preview and proper prediction for the simulation I need to create the playable and the playableBehaviour, cache it and run some methods on top of that.
I cannot reliably run the behaviour on it's own or the copy the calculation out of the clip behaviour as the clip itself can be extended and we have several versions that apply different effects to the movement.

The code I use to create the temporary graph and playable:

                    m_previewPlayableGraph = PlayableGraph.Create($"Preview graph for Transform Move Clip Inspector");

                m_previewPlayable = playableAsset.CreatePlayable(m_previewPlayableGraph, null);
                var playableType = m_previewPlayable.GetPlayableType();
                if (s_behaviourBaseType.IsAssignableFrom(playableType))
                    m_previewTransformBehaviour = ((ScriptPlayable<TransformMoveBehaviour>) m_previewPlayable).GetBehaviour();

Then OnDisable and OnDestroy I call


I presume this is the cause of that error, as when I remove that code it all compiles fine... though doesn't allow me to do the preview.

What would be the correct way of clearing that data so that there is no errors?

So I believe this is happening because Destroy() queues the playable/graph to be destroyed, but the process to do the actual destruction isn't being run before the reload occurs.

PlayableDirector has an internal method (ProcessPendingPlayableGraphs) that kicks the playable processing, but it's internal. Internally PlayableDirector calls this when it destroys the graph (similar to DestroyImmediate).

A few things to try
1) Use AssemblyReloadEvents.beforeAssemblyReload callback to call the destroy. That might work.
2) Use a custom playable asset and playable director instead of a monobehaviour to manage the graph, similar to how a timeline uses it.
3) Call the ProcessPendingPlayableGraphs using reflection. This might require the use of a temporary playabledirector.

Admittedly, none of these are good solutions, so I'd also recommend filing a bug on the issue.