[BACKPORT] better error handling for memory issues