S.sync: when and how to use it

Hi guys,

I’m working on a big project in SuperCollider with many files (I’m using “.loadRelative” method many times), hundreds of SynthDefs and buffers. In my system the whole thing is automatically loaded at PC startup but, sometimes, I see it gets stucked in one of these loading phases.

I suspect I haven’t fully understand the Server.sync mechanism and this can be the cause of the system getting stucked at startup.

I know the .sync method could be useful when we should wait for the server to complete some asyncronous tasks like loading SynthDefs and Buffers.

Are there other considerations I should keep in mind about when and how to use the sync method?
Thank you very much

The sync method should provide a hard synchronization point for all synchronous and asynchronous server actions up to that point - meaning basically, everything you ran before it should be finished when your thread resumes after the s.sync call. The only caveats I’m aware of:

  • sync may not be totally reliable if you’re using a network scsynth instance (e.g. not on your local machine), and a UDP connection (the default), because UDP doesn’t guarantee that messages are received in same order as they are sent. Workaround is to connect with TCP.
  • sync will not work as expected if you’re trying to synchronize complex chains of async actions. For example, if you have a sub-component that allocates a Buffer and then fills that buffer with sine after it’s allocated, then running e.g. subcomponent.init; s.sync; might only synchronize the FIRST action (allocate the buffer), but not the second (fill the buffer).

I wouldn’t expect s.sync to ever result in anything getting “stuck” - a sync wlll always resume once the server has finished processing its async queue. This can take a long time, of course, but the sync point is unlikely to get “lost” (this would be a severe bug).

Pragmatically: I over-use s.sync, and aggressively log things I’m loading, because I find that setup / resource allocation issues are some of the more subtle and hard to diagnose problems in SuperCollider, and it’s extremely rare that I need super-optimized setup time for a complex SC patch.

1 Like