从nodeJS模块调用libspotify注销时崩溃 [英] libspotify logout crashes when called from a nodeJS module

查看:109
本文介绍了从nodeJS模块调用libspotify注销时崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为nodeJS编写一个包装libspotify的模块.目的是编写一个Webapp,允许远程控制从Spotify播放音乐的设备.

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.

我已决定使用spshell示例来确保线程安全,并在纯C中编写"Spotify服务",以启动一个单独的线程来调用所有API函数.

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.

然后,nodeJS模块仅调用一些提供的函数来与Spotify进行交互.可以在以下位置找到该服务的代码: http://pastebin.com/KB6uwSC8 新线程开始于底部.

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.

现在,如果我在这样的简单程序中调用它(fget只是为了提供一种简单的登录方式).我使用c ++来获得与node-gyp编译代码一样接近的结果.

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.

如果我在nodeJS中使用完全相同的服务"(这意味着我只调用login()logout()并且不执行其他任何操作),则登出时有时会崩溃,例如7-8/10次.我尝试了很多东西,包括:

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:

  • 将编译器标志从node-gyp复制到我的小示例中
  • 摆弄Spotify线程的线程属性
  • 在OSX和Debian上编译
  • 使用libuv代替普通的pthreads
  • 将我的服务"编译到共享库,然后从节点调用它

无济于事.它只是崩溃.从gdb中调用时,崩溃似乎较少,但这可能是随机的.

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

来自gdb的堆栈跟踪显示以下内容:

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 ?? ()

(在OSX gdb中,在libspotify中调用的函数称为"process_title".)

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

由于到目前为止没有任何帮助,所以我只是不知道我是否可以使它工作,或者libspotify中是否与nodeJS不兼容.我不明白node-gyp如何链接.o文件,也许出了什么问题?

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?

我在github上找到了另外两个尝试执行此操作的项目,但是其中一个项目实际上将spotify主循环放入Javascript中,另一个项目使用node 0.1.100和libspotify 0.0.4,但尚未在2中进行更新年.我不能从他们两个那里学到任何东西.

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.

推荐答案

好的,我已经玩了很多.我只是忽略了注销错误,而是继续实现其他功能.

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

我在logging_in回调中添加了一个新的sp_playlist_container创建,这显然有所帮助.之后,节点模块不再崩溃(或尚未崩溃).

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);
}  

但是创建sp_playlist_container 必须在login_in回调中,如果我在另一个函数(例如"getPlaylistNames")中调用它,程序也会崩溃.

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.

这篇关于从nodeJS模块调用libspotify注销时崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆