Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

httpcOpenContext fails after exactly 8 times, consistently #66

Closed
zeta0134 opened this issue Dec 14, 2014 · 11 comments
Closed

httpcOpenContext fails after exactly 8 times, consistently #66

zeta0134 opened this issue Dec 14, 2014 · 11 comments

Comments

@zeta0134
Copy link

When opening http contexts to perform file downloads, every attempt after the 8th fails consistently, with the same error code, somewhere during the call to httpcOpenContext.

To reproduce this, I modified the http download example, and got it to repeatedly download files in a loop. It's hosted here: http://darknovagames.com:1337/3ds/http_context_test.zip. This is fairly straightforward; run the code, press A repeatedly, and after the 8th call to httpcOpenContext(), it fails consistently.

I'm testing on a 3DS XL, using firmware version 9.2.0-20U. This is through Ninjhax; I lack the ability to test on anything else. This is pretty weird; it feels like a resource is not being released somewhere, but httpcCloseContext() is definitely being called.

@plutooo
Copy link
Collaborator

plutooo commented Dec 14, 2014

Could you paste your test code? Thanks.

@zeta0134
Copy link
Author

Sure, here's the contents of main.cpp.

#include <stdlib.h>
#include <string.h>

#include <string>

using namespace std;

#include <3ds.h>

Result http_download(httpcContext *context)//This error handling needs updated with proper text printing once ctrulib itself supports that.
{
    Result ret=0;
    u8* framebuf_top, *framebuf_bottom;
    u32 statuscode=0;
    u32 size=0, contentsize=0;
    u8 *buf;

    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memset(framebuf_bottom, 0x40, 240*320*3);
    gfxFlushBuffers();
    gfxSwapBuffers();

    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memset(framebuf_bottom, 0x40, 240*320*3);
    gfxFlushBuffers();
    gfxSwapBuffers();
    gspWaitForVBlank();

    ret = httpcBeginRequest(context);
    if(ret!=0)return ret;

    ret = httpcGetResponseStatusCode(context, &statuscode, 0);
    if(ret!=0)return ret;

    if(statuscode!=200)return -2;

    ret=httpcGetDownloadSizeState(context, NULL, &contentsize);
    if(ret!=0)return ret;

    buf = (u8*)malloc(contentsize);
    if(buf==NULL)return -1;
    memset(buf, 0, contentsize);

    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memset(framebuf_bottom, 0xc0, 240*320*3);
    gfxFlushBuffers();
    gfxSwapBuffers();

    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memset(framebuf_bottom, 0xc0, 240*320*3);
    gfxFlushBuffers();
    gfxSwapBuffers();
    gspWaitForVBlank();

    framebuf_top = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);

    ret = httpcDownloadData(context, buf, contentsize, NULL);
    if(ret!=0)
    {
        free(buf);
        return ret;
    }

    size = contentsize;
    if(size>(240*400*3))size = 240*400*3;

    memset(framebuf_bottom, 0xff, 240*320*3);
    memcpy(framebuf_top, buf + 8, size - 8); //skip past the width/height markers

    gfxFlushBuffers();
    gfxSwapBuffers();

    framebuf_top = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
    framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);

    memset(framebuf_bottom, 0xff, 240*320*3);
    memcpy(framebuf_top, buf + 8, size - 8);

    gfxFlushBuffers();
    gfxSwapBuffers();
    gspWaitForVBlank();

    free(buf);

    return 0;
}

void display_image(u8* data, int size) {
    u8* framebuf_bottom = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
    memcpy(framebuf_bottom, data + 8, size - 8); //skip past the width/height markers
}

void download_and_display_image(string url) {
    //debug_message("Attempting to download: " + url);
    Result ret=0;
    httpcContext context;
    ret = httpcOpenContext(&context, (char*)url.c_str(), 0);//Change this to your own URL.

    if(ret==0)
    {
        ret=http_download(&context);
        httpcCloseContext(&context);
    }
}

int main()
{
    // Initialize services
    srvInit();
    aptInit();
    hidInit(NULL);
    gfxInit();
    //gfxSet3D(true); // uncomment if using stereoscopic 3D
    httpcInit();

    string image_list[3] = {"http://darknovagames.com:1337/3ds/raw1.bin","http://darknovagames.com:1337/3ds/raw2.bin","http://darknovagames.com:1337/3ds/raw3.bin"};
    int current_image = 0;

    // Main loop
    while (aptMainLoop())
    {
        gspWaitForVBlank();
        hidScanInput();

        // Your code goes here

        u32 kDown = hidKeysDown();
        if (kDown & KEY_START) {
            break; // break in order to return to hbmenu
        }

        if (kDown & KEY_A) {
            //download an image!
            download_and_display_image(image_list[current_image]);
            current_image++;
            current_image = current_image % 3;
        }

        // Flush and swap framebuffers
        gfxFlushBuffers();
        gfxSwapBuffers();
    }

    // Exit services
    httpcExit();
    gfxExit();
    hidExit();
    aptExit();
    srvExit();
    return 0;
}

plutooo pushed a commit that referenced this issue Dec 15, 2014
Slashmolder added a commit to Slashmolder/ctrulib that referenced this issue Dec 16, 2014
Slashmolder added a commit to Slashmolder/ctrulib that referenced this issue Dec 30, 2014
Slashmolder added a commit to Slashmolder/ctrulib that referenced this issue Dec 30, 2014
WinterMute added a commit that referenced this issue Feb 1, 2015
@zeta0134
Copy link
Author

zeta0134 commented Feb 2, 2015

This is confirmed fixed on my end.

@zeta0134 zeta0134 closed this as completed Feb 2, 2015
@celcodioc
Copy link

It seems to me that this has not been fixed yet: filiphsandstrom/DownloadMii-3DS#24

Looks like everyone is experiencing this; I get return code -660561894 (0xd8a0a01a) when calling httpcOpenContext the 9th+ time.

@zeta0134's modified example app doesn't work properly either for me, so I'm not sure what's going on. I'm using Ninjhax 1.1 (3DS XL, 9.2.0-20E), and the latest libctru commit (9fec42f).

@WinterMute
Copy link
Member

Are you absolutely sure that you're using the latest libctru? @zeta0134's test code is working perfectly for me on the same setup with this patch (Ninjhax 1.1, 3DS XL, 9.2.0-20E). Is there some other version of that code that's failing?

@plutooo
Copy link
Collaborator

plutooo commented Feb 11, 2015

Also can you check return value of every single httpcCloseContext? Maybe your code is leaking contexts.

@plutooo
Copy link
Collaborator

plutooo commented Feb 12, 2015

Read my comment again.

@celcodioc
Copy link

It always closes with code 0 on my build, I assume that's what's supposed to happen?

Also, now that I'm getting the return codes from httpcCloseContext, for some reason the ninth httpcOpenContext doesn't even return anything; it simply freezes the 3DS (how that's related to storing that return value I have no idea). No problems running the apps in 3dmoo, except these messages when calling httpcCloseContext:

>> svcSendSyncRequest (0x32)
http_c_SyncRequest (line 217): CloseContext 0dadbb2a --stub--

>> svcCloseHandle (0x23)
svcCloseHandle: svcCloseHandle undefined for handle-type "service".

@plutooo
Copy link
Collaborator

plutooo commented Feb 12, 2015

@filfat Me too. httpcCloseContext() can fail (return non-zero) and thus leak handles.

@celcodioc Yeah, that's correct. Sounds like you might be leaking something/somewhere else. Maybe you could modify 3dmoo to verify that all handles are closed/cleaned up correctly.

encounter pushed a commit to encounter/ctrulib that referenced this issue Aug 17, 2015
encounter pushed a commit to encounter/ctrulib that referenced this issue Aug 17, 2015
encounter pushed a commit to encounter/ctrulib that referenced this issue Aug 17, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@WinterMute @zeta0134 @plutooo @celcodioc and others