Repository navigation
Description
While attempting to run the pyquil test suite in parallel via the pytest-xdist plugin, I noticed occasional "Unhandled memory fault" errors like the following.
Based on the error message, this looked similar to quil-lang/qvm#110, so I tried disabling logging in quilc and, sure enough, the errors disappeared.
pyquil/tests/test_operator_estimation.py:1303:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
pyquil/operator_estimation.py:938: in measure_observables
calibr_results, d_calibr_qub_idx = _exhaustive_symmetrization(qc, qubs_calibr, calibr_shots, calibr_prog)
pyquil/operator_estimation.py:1083: in _exhaustive_symmetrization
total_prog_symm_native = qc.compiler.quil_to_native_quil(total_prog_symm)
pyquil/api/_error_reporting.py:238: in wrapper
val = func(*args, **kwargs)
pyquil/api/_compiler.py:340: in quil_to_native_quil
response = self.client.call('quil_to_native_quil', request, protoquil=protoquil).asdict() # type: Dict
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <rpcq._client.Client object at 0x1235b57f0>, method_name = 'quil_to_native_quil', rpc_timeout = 10
args = (NativeQuilRequest(_type='NativeQuilRequest', quil='PRAGMA READOUT-POVM 0 "(0.95 0.18000000000000005 0.050000000000000...]\nMEASURE 0 ro[0]\n', target_device=TargetDevice(_type='TargetDevice', isa={'1Q': {'0': {}}, '2Q': {}}, specs=None)),)
kwargs = {'protoquil': None}
request = RPCRequest(_type='RPCRequest', id='bc69a5f1-57e2-47f5-96af-0eab678bebc4', jsonrpc='2.0', method='quil_to_native_quil',...nMEASURE 0 ro[0]\n', target_device=TargetDevice(_type='TargetDevice', isa={'1Q': {'0': {}}, '2Q': {}}, specs=None)),)})
start_time = 1567443910.04178, timeout = 9999.999046325684
raw_reply = b'\x85\xa7jsonrpc\xa32.0\xa5error\xbeUnhandled memory fault at #x0.\xa2id\xda\x00$bc69a5f1-57e2-47f5-96af-0eab678bebc4\xa8warnings\x90\xa5_type\xa8RPCError'
reply = RPCError(error='Unhandled memory fault at #x0.', id='bc69a5f1-57e2-47f5-96af-0eab678bebc4', jsonrpc='2.0', warnings=[])
The work-around in qvm-app was to add a WITH-LOCKED-LOG macro and use it to acquire a global lock around any locking calls.
In the case of RPCQ, it's not so simple since the logger instance is passed in by the caller of RPCQ:START-SEVER.
Ideally, this would be resolved in CL-SYSLOG, if possible, but we might want to implement a workaround in QUILC/RPCQ in case that turns out to be impossible / impractical / slow to get merged.
Here is a minimal-ish testcase that reproduces the issue.
(ql:quickload :rpcq)
(defun test-method ()
"hey")
(let* ((number-of-workers 4)
(addr (format nil "inproc://~a" (uuid:make-v4-uuid)))
(server-function
(lambda ()
(let ((dt (rpcq:make-dispatch-table)))
(rpcq:dispatch-table-add-handler dt 'test-method)
(rpcq:start-server :dispatch-table dt
:listen-addresses (list addr)
:logger (make-instance 'cl-syslog:rfc5424-logger
:app-name "logtest"
:facility ':local0
:maximum-priority ':debug
:log-writer
#-windows (cl-syslog:tee-to-stream
(cl-syslog:syslog-log-writer "logtest" :local0)
*error-output*))))))
(server-thread (bt:make-thread server-function)))
(sleep 1)
(let ((threads '()))
(unwind-protect
(loop :repeat number-of-workers :do
(push (bt:make-thread (lambda ()
(loop :repeat 20 :do
(rpcq:with-rpc-client (client addr)
(rpcq:rpc-call client "test-method")))))
threads))
(progn
(dolist (thread threads)
(bt:join-thread thread))
(bt:destroy-thread server-thread)))))
Activity
appleby commentedon Sep 3, 2019
Simpler reproduction that doesn't involve RPCQ or
CL-SYSLOG:RFC5424-LOGGER, justCL-SYSLOG:LOG.appleby commentedon Sep 3, 2019
The plot thickens! That last test does not reproduce on linux laptop (with older SBCL). My linux laptop is only dual-core (4x logical) though, so maybe that's the reason.
I'll try installing a linux VM on my macbook and see if it reproduces there.
appleby commentedon Sep 3, 2019
Neither of the above tests reproduce on the latest
rigetti/rpcqdocker image running on my macbook, which reports:appleby commentedon Sep 3, 2019
man 3 aslon my macbook saysNot sure I am looking in the correct place (or correct version), but Apple's syslog wrapper here appears to maintain a global asl client and doesn't appear to add any locking around it for
syslogcalls.appleby commentedon Sep 4, 2019
Backtrace of failure on Mac OS from the bordeaux-threads-only testcase:
appleby commentedon Sep 5, 2019
Dumping state before context-switch. Need some time to ponder the correct fix.
Long story short: it looks like if you want thread-safe syslogging on macOS, you have to do the locking yourself. You thought POSIX would protect you? Muuuhahahaa. Fools!
Strangely
openlog,closelog, andsetlogmaskdo manage the locking for you...not sure whysyslogwas left out of thepthread_mutex_lockparty.Silver lining: you can get a thread-safe (I think)
syslog(3)compatible interface by calling_syslog$DARWIN_EXTSNrather than plain old_syslog. If you write a C program on macOS that callssyslog, the compiler does somedefine+__asmmagic and arranges to tell the assembler that even though you spelled itsyslogyou really wanted_syslog$DARWIN_EXTSN.Observe:
Indeed the
syslog.hheader is renaming the symbol for us.And in the Apple syslog sources in
syslog.cwe findIn other words, calling (the mach-o-object symbol)
_sysloggets yousyslog_legacy, which winds up calling legacyasl(3)APIs in a non-thread-safe way. Whereas calling (the mach-o-object symbol)_syslog$DARWIN_EXTSNgets yousyslog_os_log, which instead callsos_log_with_args, which appears to be thread safe (I couldn't make it crash) but I'm not certain because I couldn't find sources for Apple'sos_log(3)APIs anywhere.Tying this back in to
cl-syslog: the core ofcl-syslogis a pretty thin wrapper over good ol'syslog, and is defined like so:If I apply the following patch, then memory corruption woes disappear when
cl-sysloging on SBCL/macOS:That
syslog$DARWIN_EXTSNis gross, but would be more palatable if there is a way to grovel the desired symbol name forsyslogout of the compiler/linker.Weird fact no. 47: calling
syslog$DARWIN_EXTSNdoesn't appear to actually send messages tosyslogd? I'm still not 100% sure about that because macOS makes it surprisingly difficult to killsyslogd.launchctlkeeps restarting it, but doesn't give you the option to shut the service down. In my desperation, I even triedmving the binary aside to thwartlaunchctl, but got an "operation not permitted", even though I was root! I never thought I'd say it, but I misssystemd. But I digress. The point is that messages logged withsyslog$DARWIN_EXTSNdo appear in Console.app, but do not appear in/var/log/system.log, which I think means they aren't actually sent tosyslogd, but maybe that is neither here nor there. My point is that it would be slightly awkward for a library calledcl-syslogif wasn't actually sending messages tosyslogd... On the other hand, this is exactly what you get when you callsyslogfrom a C source file on macOS, so maybe it's not so bad.appleby commentedon Sep 30, 2019
Did some more digging on this today. The good news, I was able to use cffi-grovel's
DEFWRAPPERto hack around the issue ofsyslogvssyslog$DARWIN_EXTSNon recent macOS in a way that is slightly less disgusting than hard codingsyslog_$DARWIN_EXTSNas the foreign name.The bad news: it appears that
syslog$DARWIN_EXTSNis not thread safe either, although it is harder to trigger the bug viasyslog$DARWIN_EXTSNthan viasyslog, possibly because the$DARWIN_EXTSNversion replaces the unsafe_asl_lib_vlogwith the (maybe safe? source not available)os_log_with_args, but both versions still call_asl_evaluate_send, which is apparently not safe.For the record, here is the proof-of-concept
CL-SYSLOGpatch that installs a syslog wrapper and arranges to call it instead of_syslogon#+DARWIN, which, as mentioned above, mitigates but does not fix the problem :(.