libspotify logout crashes when called from a nodeJS module

杀马特。学长 韩版系。学妹 提交于 2019-12-10 22:29:08

问题


I am trying to write a module for nodeJS which wraps libspotify. The goal is to write a webapp that allows the remote control of a device playing music from spotify.

I have decided to go along the spshell example to ensure thread safety and write a "Spotify Service" in plain C that starts a seperate thread which calls all the API functions.

The nodeJS module then just calls a few provided functions to interact with spotify. The code for the service can be found here: http://pastebin.com/KB6uwSC8 The new thread gets started at the bottom.

Now, if i call this in a simple program like this (the fget is just to have a simple way for the login to complete). I used c++ to get as close to as node-gyp compiles the code.

#include <stdio.h>

extern "C" {
        #include "objects/SpotifyService.h"
}

int  main(int argc, char** argv) {
    login();
    char string[100];
    fgets(string, 100, stdin);
    fprintf(stdout, "Got: %s", string);
    logout();
    fgets(string, 100, stdin);
    fprintf(stdout, "Got: %s", string);
    return 0;
}

It works fine. I can't get this to crash.

If I use the same exact "Service" in nodeJS (meaning I just call login() and logout() and do nothing else), it crashes sometimes when logging out, like 7-8/10 times. I've tried lots of stuff, including:

  • Copying the compiler flags from node-gyp to my small example
  • fiddling with the thread attributes of the spotify thread
  • compiling on OSX and Debian
  • using libuv instead of plain pthreads
  • compiling my "service" to a shared library and call this from node

to no avail. It just crashes. It seems to crash less when called from within gdb, but that could be random.

A stack trace from gdb shows the following:

Thread 3 (Thread 0x7ffff65fd700 (LWP 21838)):
#0  0x00007ffff678f746 in ?? () from /usr/local/lib/libspotify.so.12
#1  0x00007ffff6702289 in ?? () from /usr/local/lib/libspotify.so.12
#2  0x00007ffff6702535 in ?? () from /usr/local/lib/libspotify.so.12
#3  0x00007ffff6703b5a in ?? () from /usr/local/lib/libspotify.so.12
#4  0x00007ffff6703c86 in ?? () from /usr/local/lib/libspotify.so.12
#5  0x00007ffff66c5c8b in ?? () from /usr/local/lib/libspotify.so.12
#6  0x00007ffff679a5b3 in sp_session_process_events () from /usr/local/lib/libspotify.so.12
#7  0x00007ffff6aa7839 in spotifyLoop (nervNicht=<value optimized out>) at    ../src/SpotifyService.c:103
#8  0x00007ffff70118ca in start_thread () from /lib/libpthread.so.0
#9  0x00007ffff6d78b6d in clone () from /lib/libc.so.6
#10 0x0000000000000000 in ?? ()

(In OSX gdb showed that the function called in libspotify is called "process_title".)

Since nothing has helped so far i just don't have any idea if i can get this to work or if it is something in libspotify that's just incompatible with nodeJS. I don't understand how node-gyp links the .o files, maybe there something goes wrong?

I found two other projects on github that try to do this, but one of them puts the spotify main loop actually in Javascript and the other one uses node 0.1.100 and libspotify 0.0.4 and hasn't been updated in 2 years. I couldn't learn anything from both of them.


回答1:


OK, i've played around some more. I just ignored the logout error and continued to implement other features.

I added a new sp_playlist_container creation in the logged_in callback and apparently that helped. After that, the node module does not crash anymore (or hasn't yet).

static sp_playlistcontainer_callbacks pc_callbacks = {
     .container_loaded = &rootPlaylistContainerLoaded,
};

static void rootPlaylistContainerLoaded(sp_playlistcontainer* pc, void* userdata) {
    int numPlaylists = sp_playlistcontainer_num_playlists(pc);
    fprintf(stdout, "Root playlist synchronized, number of Playlists: %d\n", numPlaylists);
}

static void loggedIn(sp_session* session, sp_error error) {
    if(SP_ERROR_OK != error) {
            fprintf(stderr, "Error logging in: %s\n", sp_error_message(error));
    } else {
            fprintf(stdout, "Service is logged in!\n");
    } 

    //This is absolutely necessary here, otherwise following callbacks can crash.
    sp_playlistcontainer *pc = sp_session_playlistcontainer(spotifySession);
    sp_playlistcontainer_add_callbacks(pc, &pc_callbacks, NULL);
}  

But the sp_playlist_container creation must be in the logged_in callback, if i called it in another function (say, "getPlaylistNames") the program crashed, too.

I'll see if it continues to work and hope this answer can help others.



来源:https://stackoverflow.com/questions/14633511/libspotify-logout-crashes-when-called-from-a-nodejs-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!